Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand Django's sorl-thumbnail

I have been playing around with sorl-thumbnail for Django. And trying to understand how it works better.

I've read the guide for it, installed it in my site-packages, made sure PIL is installed correctly, put sorl.thumbnail in the INSTALLED APPS in my settings.py, put from sorl.thumbnail.fields import ImageWithThumbnailsField at the top in my models.py, added image = ImageWithThumbnailsField(upload to="images/", thumbnail={'size':(80, 80)}) as one of my model fields, passed the model through my view to the template, and in the template added {% load thumbnail %} at the top and put in the variable {{ mymodel.image.thumbnail_tag }} in there too.

But from what I understood is that when I upload an image through the admin, it would create the thumbnail straight away, but it only actually creates in when I see my template in the browser? Is this correct? The thumbnail shows fine, it looks great in fact, but I thought that adding the model field part of it would create the thumbnail instantly once the image has uploaded? ...Why not just use the models.ImageField in my model instead?

...or have I done this all OK and I've just got the way it works wrong?

like image 306
littlejim84 Avatar asked Jul 23 '09 13:07

littlejim84


1 Answers

I'm one of the sorl-thumbnail developers.

Firstly, you don't need to {% load thumbnail %} unless you're just using the thumbnail tag rather than a thumbnail field.

Currently, a thumbnail is only ever created the first time it is used - even if you use the field [I'll get around to changing that one day if no-one else does first]. The advantage of the field is that you can specify the sizing rather than giving the freedom to the designer in the template level [and making it easier for an admin thumbnail].

Both ways work, you get to decide which works best for you.

like image 83
SmileyChris Avatar answered Oct 19 '22 10:10

SmileyChris