← Back to Django Course | Chapter 3: Templates & Template Language | Lesson 4 of 10

Template Tags Basics

Template tags are the special commands, like {% if %} and {% for %}, that let your HTML page make decisions and repeat itself.

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'.

markup
<!-- 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.

markup
<!-- 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>.

markup
<!-- 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. #}
Common Mistakes
  1. Forgetting the closing tag, e.g. writing {% if %} without a matching {% endif %}, which raises a TemplateSyntaxError.
  2. Using Python comparison operators like == inside {% if %} correctly but forgetting that and/or/not -- not && / || -- are the valid boolean keywords.
  3. Nesting {% for %} loops without giving each loop a distinct variable name, causing confusing variable shadowing.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.