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 %}
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
Similar Reads
Django Templates
Templates are the third and most important part of Django's MVT Structure. A Django template is basically an HTML file that can also include CSS and JavaScript. The Django framework uses these templates to dynamically generate web pages that users interact with. Since Django primarily handles the ba
7 min read
variables - Django Templates
Prerequisite- Django TemplatesIn Django, templates are used to dynamically generate HTML content by combining static HTML with dynamic data from views. One of the simplest and most useful features of Django templates is the use of variables. Variables allow you to display data passed from a view ins
2 min read
Django Template Tags
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. Templ
4 min read
extends - Django Template Tags
Djangoâs {% extends %} tag allows you to reuse a base template across multiple pages, so you donât have to repeat the same HTML code in every template. This makes your templates cleaner, easier to maintain, and helps keep a consistent layout throughout your site.Syntax: {% extends 'base_template.htm
2 min read
if - Django Template Tags
The {% if %} tag in Django templates allows us to control what content is displayed based on certain conditions. We can use it to show or hide parts of a page depending on whether a condition is met.Syntax of the {% if %} Tag{% if variable %}// statements{% else %}// statements{% endif %}condition:
3 min read
for loop - Django Template Tags
Django templates allow you to render dynamic data by embedding Python-like logic into HTML. The for loop is one of the most commonly used template tags, enabling you to iterate over lists or other iterable objects. It helps you display repeated content (like lists, tables, etc.) in your templates wi
3 min read
comment - Django template tags
A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to the template but also provide som
2 min read
include - Django Template Tags
A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
2 min read
url - Django Template Tag
A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
3 min read
cycle - Django Template Tags
A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
3 min read