Examples¶
Template examples¶
All of the examples assume that you first load the thumbnail template tag in
your template:
{% load thumbnail %}
Simple:
{% thumbnail item.image "100x100" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}
Crop using margin filter, x, y aliases:
{% thumbnail item.image "100x700" as im %}
<img style="margin:{{ im|margin:"100x700" }}" src="{{ im.url }}" width="{{ im.x }}" height="{{ im.y }}">
{% endthumbnail %}
Using external images and advanced cropping:
{% thumbnail "https://example.com/logo.png" "40x40" crop="80% top" as im %}
<img src="{{ im.url }}">
{% endthumbnail %}
Using the empty feature, the empty section is rendered when the source is resolved to an empty value or an invalid image source, you can think of it as rendering when the thumbnail becomes undefined:
{% thumbnail item.image my_size_string crop="left" as im %}
<img src="{{ im.url }}">
{% empty %}
<p>No image</p>
{% endthumbnail %}
Nesting tags and setting size (geometry) for width only:
{% thumbnail item.image "1000" as big %}
{% thumbnail item.image "50x50" crop="center" as small %}
<a href="{{ big.url}}" title="look ma!"><img src="{{ small.url }}"></a>
{% endthumbnail %}
{% endthumbnail %}
Setting geometry for height only:
{% thumbnail item.image "x300" as im %}
<img src="{{ im.url }}">
{% endthumbnail %}
Setting format and using the is_portrait filter:
{% if item.image|is_portrait %}
<div class="portrait">
{% thumbnail item.image "100" crop="10px 10px" format="PNG" as im %}
<img src="{{ im.url }}">
{% endthumbnail %}
</div>
{% else %}
<div class="landscape">
{% thumbnail item.image "50" crop="bottom" format="PNG" as im %}
<img src="{{ im.url }}">
{% endthumbnail %}
</div>
<div>
<p>Undefined behaviour</p>
</div>
{% endif %}
Using HTML filter:
{{ text|html_thumbnails }}
Using markdown filter:
{{ text|markdown_thumbnails }}
Model examples¶
Using sorl.thumbnail.ImageField in your models:
from django.db import models
from sorl.thumbnail import ImageField
class Item(models.Model):
image = ImageField(upload_to='whatever')
Note
You do not need sorl.thumbnail.ImageField to make thumbnails;
the standard django.db.models.ImageField works. The sorl field adds
the admin widget explained in the next section, and validates uploads
with the thumbnail engine: a corrupt or truncated file is rejected with
“Upload a valid image”, and the file input gets accept="image/*".
Another example on how to use sorl.thumbnail.ImageField in your existing
project with only small code changes:
# util/models.py
from django.db.models import *
from sorl.thumbnail import ImageField
# myapp/models.py
from util import models
class MyModel(models.Model):
logo = ImageField(upload_to='/dev/null')
Admin examples¶
Recommended usage using sorl.thumbnail.admin.AdminImageMixin (note that this requires use of sorl.thumbnail.ImageField in your models as explained above):
# myapp/admin.py
from django.contrib import admin
from myapp.models import MyModel
from sorl.thumbnail.admin import AdminImageMixin
class MyModelAdmin(AdminImageMixin, admin.ModelAdmin):
pass
And the same thing For inlines:
# myapp/admin.py
from django.contrib import admin
from myapp.models import MyModel, MyInlineModel
from sorl.thumbnail.admin import AdminImageMixin
class MyInlineModelAdmin(AdminImageMixin, admin.TabularInline):
model = MyInlineModel
class MyModelAdmin(admin.ModelAdmin):
inlines = [MyInlineModelAdmin]
Easy to plugin solution example with little code to change:
# util/admin.py
from django.contrib.admin import *
from sorl.thumbnail.admin import AdminImageMixin
class ModelAdmin(AdminImageMixin, ModelAdmin):
pass
class TabularInline(AdminImageMixin, TabularInline):
pass
class StackedInline(AdminImageMixin, StackedInline):
pass
# myapp/admin.py
from util import admin
from myapp.models import MyModel
class MyModelAdmin(admin.ModelAdmin):
pass
Low level API examples¶
How to get make a thumbnail in your python code:
from sorl.thumbnail import get_thumbnail
im = get_thumbnail(my_file, '100x100', crop='center', quality=99)
How to delete a file, its thumbnails as well as references in the Key Value Store:
from sorl.thumbnail import delete
delete(my_file)
Catching a source that can’t be read¶
When the source is missing from the storage, or the engine can’t open it as an
image, get_thumbnail logs the error and returns a thumbnail that was never
written: its url points to a file that isn’t there. Pass
raise_errors=True to get the exception and show your own fallback:
from django.templatetags.static import static
from sorl.thumbnail import get_thumbnail
try:
url = get_thumbnail(user.photo, '135x135', crop='center', raise_errors=True).url
except OSError:
url = static('img/person-outline.png')
The exception is the one the storage or the engine raised. A missing file is
the storage’s error: FileNotFoundError for files on disk, a remote storage
may raise its own. A file that is not an image is the engine’s error: with PIL
UnidentifiedImageError, also an OSError. The convert engine only finds
that out when it runs ImageMagick, so it raises EngineError with or
without this option.
With THUMBNAIL_DUMMY on you get the dummy image, also with
raise_errors=True. Turn the setting off to see the error.
The source is only read when the thumbnail has to be made. A thumbnail that is already in the key-value store is returned as it is, also when its source was deleted since, see How sorl-thumbnail operates.
sorl-thumbnail 14.0 will raise by default. Until then a source that can’t be
read emits a FutureWarning when raise_errors is not given. Pass
raise_errors=False to keep the thumbnail that was never written, without
the warning.
Reading a source once in a job¶
Within a request sorl-thumbnail downloads a source once however many
thumbnails and filters use it (see Reading the source once). A management command
or a background job has no request, so wrap the work for each image in
source_cache:
from sorl.thumbnail import get_thumbnail, source_cache
for item in Item.objects.all():
with source_cache():
for geometry in ('100x100', '400x400', '800x800'):
get_thumbnail(item.image, geometry)
The block keeps the last source read and drops it when the block ends, also when it raises.