I want to upload image with width 400px and get two other images - 200px and 100px. I googled and django-imagekit looks like it can do this.
So I added imagekit
to INSTALLED_APPS and added this to my model:
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFit
class Bookmaker(models.Model):
...
logo_large = models.ImageField(upload_to="logo/large")
logo_medium = ImageSpecField([ResizeToFit(width=200)], source='logo_large', options={'quality': 90})
logo_small = ImageSpecField([ResizeToFit(width=100)], source='logo_large', options={'quality': 90})
And this is my form:
@staff_member_required
def new(request):
if request.method == 'POST':
form = BookmakerForm(request.POST, request.FILES)
if form.is_valid():
form.save()
else:
form = BookmakerForm()
template = loader.get_template('bookmaker/new.html')
context = RequestContext(request, {
'form': form,
})
return HttpResponse(template.render(context))
I've gone through docs but I could not understand how imagekit works. Do I need to setup CACHE in my app? Does it create files physically in MEDIA
folder?
Well my setup does not work. When I upload image logo_large
, large file is created, but logo_small
and logo_medium
are not. If I print {{bookmaker.logo_small}}
I get media/CACHE/images/logo/large/bookmaker/24d9f99ffdf277ed3fc25e2cf4d37554.jpg
, which does not exist (404). Do I need to setup CACHE to make it work?
Hm, it works now. I did not do anything. Probably just takes some time to generate those images (I do not know what it takes to generate images), probably restart ./manage.py runserver
is enough.
May be I need to call ./manage.py generateimages
after every upload?