Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping Conditions in Django template if statement

I would like to negate the result of a group of conditions separated 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: Could not parse the remainder: '(owner.home_number' from '(owner.home_number'

like image 797
Crystal Avatar asked May 13 '14 17:05

Crystal


1 Answers

Since the order of evaluation is the following:

  • or
  • and
  • not

you can omit the parenthesis:

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

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

{% if owner.home_number or owner.work_number or owner.mobile_number %}
{% else %}
    No contact number available
{% endif %}
like image 168
alecxe Avatar answered Oct 14 '22 07:10

alecxe