How to Write Conditional if Statements in Django Templates
Let's say we have a boolean field in our model called active
and would like to do something in a template if the value is True
. We would need to write an if
conditional statement and evaluate the value of object.active
, which can be done like this:
Template if Statement
{% if object.active %}
#True, do something here...
{% endif %}
Template if-else Statement
An if-else
statement can be applied to templates like this:
{% if object.active %}
#True, do something here...
{% else %}
#Else do something here...
{% endif %}
django