About ANDs and ORs in Django Templates

ANDs precede ORs in the conditions of most programming languages. So these following two lines are equivalent in Python:

1
2
condition1 and condition2 or condition3 and condition4
(condition1 and condition2) or (condition3 and condition4)

Django template language doesn't have a concept of parentheses in conditions, so the above condition would look like this:

1
2
3
{% if condition1 and condition2 or condition3 and condition4 %}
    ...
{% endif %}

To have ORs executed before ANDs in Django templates, use nesting:

1
2
3
4
5
{% if condition1 or condition2 %}
    {% if condition3 or condition4 %}
        ...
    {% endif %}
{% endif %}

Tips and Tricks Programming Django 4.2 Django 3.2 Django 2.2 Python 3