I've got a portion of my app that allows a custom logo.
Default object (and object view) state is no custom logo, but if one exists, replace the header with a custom logo navbar.
logo is a standard ImageField on co(mpany) model:
models.py:
class Co(models.Model): logo = models.ImageField(blank=True) template:
{% if co.logo %} {% block navbar %} {% include 'templates/navbar_logo.html' %} {% endblock %} {% endif %} I've also tried this with {% if co.logo == None %}, {% if not ... %}, {% if co.logo.url %} and trying to model the logic with co.logo|default_if_none:"" but in instances where a logo isn't set the view throws:
ValueError at /co/foo The 'logo' attribute has no file associated with it.
for... empty also doesn't work
{% for co.logo.url in co %} ... {% empty %} ... {% endfor %} in django shell:
(with logo)
c.logo >> <ImageFieldFile: logo.png> c.logo.url >> '/media/logo.png' (no logo)
b.logo >> <ImageFieldFile: None> is there a built in django template tag filter that will allow this condition to pass True only if the field has an image uploaded? otherwise don't load the nav block? thanks