[Solved]-Django – Things to know about sorl-thumbnail


Like many things in django sorl-thumbnail is implemented both simply and elegantly, and the way you are using it is correct.

Each time sorl-thumbnail is asked for a new thumbnail, it checks to see if one exists in its cache:

  1. If one exists, this one is returned.
  2. If it doesn’t, a new one is generated and stored, then returned.

Thus, a cache of thumbnails are generated as required. The key,value store is a means of storing the thumbnails that makes them quick to look up and retrieve. Using the steps above the thumbnails will be generated and stored as they are requested by your users.

Thumbnail generation ‘on demand’ process works well, as images are added gradually to your website, the thumbnails for the new images will be created as required, and thumbnails for older images retrieved from the store.

To store the thumbnails Sorl-thumbnail uses a combination of a database and memory cache. The database ensures that all the thumbnails are saved should the app and or web server are restarted. The thumbnails will then be loaded into the memory cache (you guessed it) on demand, and thus ensure fast loading times for the user.

To answer your questions:

  • Whether implementing it only in the template is the same, as using it in the models or the view and creating a new thumbnail for each one?

It is not possible to generate the thumbnails in the models or views, as they are generated on demand, as the user requests them. This is very efficient. I guess you could write a script to request all the thumbnails, which was run on a nightly basis to ensure all the thumbnails are generated, although this is probably not required.

  • How to configure different thumbnails for different image, as it can be done in easy_thumbnail?

All configuration is done in the {% thumbnail %} tag, if different thumbnails for the same image are generated, these will be stored in the key, value store separately.

  • How to override the default values, eg: override the value of Quality, etc

There are a list of settings here: http://sorl-thumbnail.readthedocs.org/en/latest/reference/settings.html, these should all be set in the settings.py file. The Quality is set to 95 by default, which is pretty high.

Edit – set jpeg quality in settings.py


These two additions, anywhere in the settings.py file for your project, will reduce the jpeg quality for the thumbnails to 60, and switch off the creation of progressive jpegs.

Edit – thumbnail aliases

There is no inbuilt support for thumbnail aliases in sorl-thumbnail. Probably the easiest way to implement them is as small sub templates, which can then be loaded into your main templates.

So a page template, might looks somthing like:

{% load thumbnail %}
<h3>Recent image uploads:</h3>
{% for item in recentUploads %}
    {% include "bigThumbnail.html" %}
{% endfor %}

<h3>Older image uploads</h3>
{% for item in oldUploads %}
    {% include "smallThumbnail.html" %}
{% endfor %}

The templates for the thumbnails would be stored as separate files in your template directory and could look something like:


{% thumbnail item.image "100x100" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}


{% thumbnail item.image "40x40" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}

In each of the sub templates, all the settings for your ‘aliases’ can be made. Alternatively, it would be possible to create an additional template tag to read these attributes from settings.py, this is what easy_thumbnail does, although would require a lot of programming to achieve the results of the 2 small templates above.

If you have further questions about sorl-thumbnail, I would be happy to help you with them.

Leave a comment