Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional ImageField (Django)

I'm having a problem with an ImageField in one of my models. It is set to blank=True, null=True (it is optional.)

When I loop through a list of objects and use object.thumbnail.url, I get "Caught an exception while rendering: The 'thumbnail' attribute has no file associated with it."

This only happens if no thumbnail has been uploaded, obviously.

Does anyone have any ideas on the best way to deal with that?

like image 684
John McCollum Avatar asked Nov 11 '09 15:11

John McCollum


1 Answers

Turned out it was as simple as:

{% if object.thumbnail %}
    <img src="{{ object.thumbnail.url }}" />
{% endif %}
like image 181
John McCollum Avatar answered Oct 17 '22 20:10

John McCollum