Conditionals in Django templates work just like they do in Python. The syntax is:
{% if some_conditions %}
Output this block.
{% elif other_conditions %}
Output this block.
{% else %}
Output this block.
{% endif %}
All of the Python comparison and logical operators are available:
Comparison Operators
==
– Equals.!=
– Doesn’t equal.>
– Is greater than.<
– Is less than.>=
– Is greater than or equal to.<=
– Is less than or equal to.is
– Is the same object.is not
– Is not the same object.in
– Is in a sequence. *Note that the only types of sequences you can create in the template are lists of strings, which you do by comma-separating the values:{% if animal in "elephant,giraffe,donkey" %}
Logical Operators
and
(e.g.,if a and b:
)or
(e.g.,if a or b:
)not
(e.g.,if not a:
)
Commentary
TemplateSyntaxError
.True
iffoo
is passed on the querystring and has some value.