Template Tags Basics
In this page:
Conditional Rendering with {% if %}
The {% if %} tag renders its enclosed HTML only when the condition is truthy, similar to a Python if statement.
Warning: Every {% if %} must be closed with {% endif %}.
Example: Conditional Rendering with {% if %}
Because is_premium is True, the template renders the 'Premium Member' badge; otherwise it would render 'Free Member'.
<!-- context = {'is_premium': True} -->
{% if is_premium %}
<span class="badge">Premium Member</span>
{% else %}
<span class="badge">Free Member</span>
{% endif %}
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Looping with {% for %}
The {% for %} tag repeats its enclosed HTML once per item in an iterable, just like a Python for loop.
Note: Inside a {% for %} loop, forloop.counter gives the current 1-based iteration number.
Example: Looping with {% for %}
This prints a numbered list: '1. Apple', '2. Mango', '3. Banana', using the built-in forloop.counter variable.
<!-- context = {'fruits': ['Apple', 'Mango', 'Banana']} -->
<ul>
{% for fruit in fruits %}
<li>{{ forloop.counter }}. {{ fruit }}</li>
{% endfor %}
</ul>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Handling Empty Lists with {% empty %}
{% for %} loops can include an {% empty %} clause that renders only when the iterable has zero items.
Example: Handling Empty Lists with {% empty %}
Since orders is an empty list, the {% empty %} block renders 'No orders yet.' instead of an empty <ul>.
<!-- context = {'orders': []} -->
<ul>
{% for order in orders %}
<li>{{ order.id }}</li>
{% empty %}
<li>No orders yet.</li>
{% endfor %}
</ul>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Forgetting the closing tag, e.g. writing {% if %} without a matching {% endif %}, which raises a TemplateSyntaxError.
- Using Python comparison operators like == inside {% if %} correctly but forgetting that and/or/not -- not && / || -- are the valid boolean keywords.
- Nesting {% for %} loops without giving each loop a distinct variable name, causing confusing variable shadowing.
- Template tags use {% tag %} syntax and control logic like conditionals, loops, and template composition.
- {% if %}/{% elif %}/{% else %}/{% endif %} and {% for %}/{% endfor %} are the most common tags.
- Every block tag that opens (if, for, block) must have a matching {% end... %} tag.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: