开发者

Optional ImageField (Django)

开发者 https://www.devze.com 2022-12-11 07:23 出处:网络
I\'m having a problem with an ImageField in one of my models. It 开发者_开发问答is set to blank=True, null=True (it is optional.)

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?


Turned out it was as simple as:

{% if object.thumbnail %}
    <img src="{{ object.thumbnail.url }}" />
{% endif %}
0

精彩评论

暂无评论...
验证码 换一张
取 消