Question

I have a project in django using sorl-thumbnail to generate thumbnails. I want to make it so that if a user profile doesnt have an avatar image added yet, it will show a default photo.

I tried doing this in my UserProfile model but I had problems uploading a new image when the default was present

avatar = models.ImageField(upload_to='blah', default='path/to/my/default/image.jpg')

I understand that I could use if statements in all my templates, but this would be repeating myself everywhere. Such as

{% ifequal object.avatar None %}
    <img src="DEFAULT_IMAGE" />
{% else %}
    display image
{% endifequal %}

What is best practice to show a default image when one doesnt exist already in django?

Was it helpful?

Solution

As Gabriel mention, you can use the empty tag.

{% thumbnail object.avatar "100x100" as im %}
    <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% empty %}
    <img src="DEFAULT_IMAGE">
{% endthumbnail %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top