Last Updated : 12 Jul, 2025
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' %}
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 %}
2. cycleTo check more about comment tag, visit: comment – Django template tags
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. extendsUsed 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, elseControls 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.
5. for loopTo check more about if tag, visit: if – Django Template Tags
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>
6. for with emptyTo check more about for loop tag, visit - for loop – Django Template Tags
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>
7. Boolean Operators with ifTo learn in detail, visit: for … empty loop – Django Template Tags
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>
8. firstofTo check more about Boolean Operators, visit - Boolean Operators – Django Template Tags
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" %}
9. includeTo check more about firstof tag, visit - firstof – Django Template Tags
Loads and renders another template within the current template.
Example:
{% include "foo/bar.html" %}
You can use relative paths like in extends.
10. loremTo check more about include tag, visit - include – Django Template Tags
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 #}
11. nowTo check more about lorem tag, visit - lorem – Django Template Tags
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
12. urlTo check more about now tag, visit - now – Django Template Tags
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4