Grouping conditions in a Django template if statement

I would like to deny the result of a group of conditions separated by ors in an if statement in a django template. Heres my code

{% if not (owner.home_number or owner.work_number or owner.mobile_number) %}
    No contact number available
{% endif %}

I am currently getting this error

TemplateSyntaxError: Failed to parse the remainder: '(owner.home_number' from '(owner.home_number'

+4
source share
1 answer

Since the evaluation order is as follows:

  • or
  • and
  • not

you can omit the brackets:

{% if not owner.home_number or owner.work_number or owner.mobile_number %}
    No contact number available
{% endif %}

Or, just FYI, you can also cancel the check:

{% if owner.home_number or owner.work_number or owner.mobile_number %}
{% else %}
    No contact number available
{% endif %}
+8
source

Source: https://habr.com/ru/post/1540415/


All Articles