Open In App

Django Template Tags

Last Updated : 15 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisite: What are Django Templates?

Django provides a powerful templating engine that allows us to add logic directly into our templates using template tags. These tags enable everything from control structures (like if and for loops), to inserting dynamic content, to template inheritance. Template tags are enclosed within {% %} and provide functionality that goes beyond what template variables ({{ }}) can offer.

Syntax

{% tag_name [arguments] %}

Example:

Tags are surrounded by {% and %} like this:

{% csrf_token %}

Most tags accept arguments, for example:

{% cycle 'odd' 'even' %}

Commonly Used Django Template Tags

1. Comment

Ignores everything between {% comment %} and {% endcomment %}. You can optionally add a note for documentation purposes.

Example:

{% comment "This is a note" %}
This block will not be rendered
{% endcomment %}

To check more about comment tag, visit: comment – Django template tags

2. cycle

Cycles through a list of values with each loop iteration. Useful for alternating row classes.

Example:

HTML
{% for item in items %}
    <tr class="{% cycle 'row1' 'row2' %}">{{ item }}</tr>
{% endfor %}

The first iteration produces HTML that refers to class row1, the second to row2, the third to row1 again, and so on for each iteration of the loop.

3. extends

Used for template inheritance. Allows a template to inherit from a base template.

Example: Assume the following directory structure:

dir1/
template.html
base2.html
my/
base3.html
base1.html

In template.html, the following relative paths would be valid:

{% extends "./base2.html" %}
{% extends "../base1.html" %}
{% extends "./my/base3.html" %}

To check more about extends tag, visit: extends – Django Template Tags

4. if, elif, else

Controls conditional logic within the template.

Example:

HTML
{% if athlete_list %}
    Number of athletes: {{ athlete_list|length }}
{% elif athlete_in_locker_room_list %}
    Athletes are in the locker room.
{% else %}
    No athletes.
{% endif %}

In the above, if athlete_list is not empty, the number of athletes will be displayed by the {{ athlete_list|length }} variable.

To check more about if tag, visit: if – Django Template Tags

5. for loop

Loops over each item in a list.

Example: display a list of athletes provided in athlete_list:

HTML
<ul>
{% for athlete in athlete_list %}
    <li>{{ athlete.name }}</li>
{% endfor %}
</ul>

To check more about for loop tag, visit - for loop – Django Template Tags

6. for with empty

Displays an alternate message if the list is empty.

Example:

HTML
<ul>
{% for athlete in athlete_list %}
    <li>{{ athlete.name }}</li>
{% empty %}
    <li>No athletes found.</li>
{% endfor %}
</ul>

To learn in detail, visit: for … empty loop – Django Template Tags

7. Boolean Operators with if

The {% if %} tag evaluates a variable, and if that variable is “true” (i.e. exists, is not empty, and is not a false boolean value) the contents of the block are output. One can use various boolean operators with Django If Template tag.

Example:

HTML
<ul> 
{% if score > 90 and passed %}
    Excellent!
{% endif %}
</ul> 

To check more about Boolean Operators, visit - Boolean Operators – Django Template Tags

8. firstof

It displays the first argument variable that is not “false” (i.e. exists, is not empty, is not a false boolean value, and is not a zero numeric value). Outputs nothing if all the passed variables are “false”.

Example:

{% firstof var1 var2 var3 "Default Value" %}

This is equivalent to:

{% if var1 %}
{{ var1 }}
{% elif var2 %}
{{ var2 }}
{% elif var3 %}
{{ var3 }}
{% else %}
Default Value
{% endif %}

One can also use a literal string as a fallback value in case all passed variables are False:

{% firstof var1 var2 var3 "fallback value" %}

To check more about firstof tag, visit - firstof – Django Template Tags

9. include

Loads and renders another template within the current template.

Example:

{% include "foo/bar.html" %}

You can use relative paths like in extends.

To check more about include tag, visit - include – Django Template Tags

10. lorem

Generates random "lorem ipsum" text, useful for placeholder content.

Example:

HTML
{% lorem %}  {# Outputs one paragraph #}
{% lorem 3 p %}  {# 3 paragraphs #}
{% lorem 2 w random %}  {# 2 random words #}

To check more about lorem tag, visit - lorem – Django Template Tags

11. now

Displays the current date/time using the specified format.

Example:

It is currently {% now "D d M Y" %}

Will output something like: Tue 13 May 2025

To check more about now tag, visit - now – Django Template Tags

12. url

Returns an absolute path reference (a URL without the domain name) matching a given view and optional parameters. This is a way to output links without violating the DRY principle by having to hard-code URLs in your templates.

Example:

{% url 'post-detail' post.id %}

To check more about url tag, visit - url - Django Template Tags


Next Article
Practice Tags :

Similar Reads