June 15, 2025
if condition in Django template
We can use {% if %} tag to add condition content load in Django template. This can control the flow of content rendering and display content based on the value of a variable passed in template. It also help the front end developers to create dynamic web pages. We can also use logical operators in a if condition such as 'and' / 'or' operators, similarly we used in Python. Examples are mentioned below:
if condition
{% if condition %}
{% endif %}
if elif condition
{% if student_marks > 90 %}
Grade: A
{% elif student_marks > 75 %}
Grade: B
{% else %}
Grade: C
{% endif %}