if Tag

Logic Most Useful

Documentation

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.

Logical Operators

  • and (e.g., if a and b:)
  • or (e.g., if a or b:)
  • not (e.g., if not a:)

Did we get something wrong? Is there a use case for the if tag that we should add? Please let us know.

Send Feedback

Official Documentation
This page last updated on Sept. 20, 2020, 7:16 a.m. EST