Operator calculations inside a Django template

Viewed 41

Inside a Django template I have such a phrase:

<h4>{{ webinar.name or 'Not title' }}</h4>

leading to this error:

Could not parse the remainder: ' or 'Not title'' from 'webinar.name or 'Not title''

This is because Django does not like to calculate operators inside a template. It seems that django-mathfilters does not have an or operator. I also do not like to use {% if ... %} either. Because this is a MWE. I have faced with other cases that I cannot get around them so easy.

2 Answers

Try this:

<h4>{{ webinar.name|default:'Not title' }}</h4>

Django does have operators inside templates but this won't work.

The easiest way to do this and keep you code clean is:

<h4>{% if webinar.name %}{{ webinar.name }}{% else %}No title{% endif %}</h4>
Related