How to Create Image Thumbnails in Django

The easiest way to use thumbnails in Django is to create them dynamically with the Sorl Thumbnail package. This will give you the flexibility of easily being able to create thumbnails in an unlimited number of sizes and change them at any time.


Another clever feature of Sorl Thumbnail is that it caches thumbnails once they have been generated and will re-cache them if any changes are made.



Make sure your terminal window is using the same virtual Python environment as your project then install Sorl Thumbnail using pip like this:


pip install sorl-thumbnail


Configuring Django

The next step is to configure your Django project's file. Add the following to your INSTALLED_APPS array:


   # packages


Create Thumbnails in Templates

Now you can create thumbnails in your templates like this:


{% load thumbnail %}

{% thumbnail i.image "960x540" crop="center" as im %}
  <img src="/static{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}" alt="{{ }}">
{% endthumbnail %}


In the above example, i.image is the image field from the database for the item object.