Django Template Tags
Django Web Framework ships with dozens of tags used to implement arbitrary logics right in the template. Tags look like this:
{% tag %}
. Tags are more complex than variables: Some create text in the output, some control flow by performing loops or logic, and some load external information into the template to be used by later variables. Tags provide arbitrary logic in the rendering process. For example, a tag can output content, serve as a control structure e.g. an “if” statement or a “for” loop, grab content from a database, or even enable access to other template tags.
Syntax
{% tag_name %}
Example
Tags are surrounded by {% and %} like this:
{% csrf_token %}
Most tags accept arguments, for example :
{% cycle 'odd' 'even' %}
Commonly used Tags in Django Templates
Comment
Template ignores everything between {% comment %} and {% endcomment %}. An optional note may be inserted in the first tag. For example, this is useful when commenting out code for documenting why the code was disabled.
Example{% comment "Optional note" %}
To check more about comment tag, visit – comment – Django template tags
Commented out text with {{ create_date|date:"c" }}
{% endcomment %}cycle
It produces one of its arguments each time this tag is encountered. The first argument is produced on the first encounter, the second argument on the second encounter, and so forth. Once all arguments are exhausted, the tag cycles to the first argument and produces it again.
Example This tag is particularly useful in a loop:html {% for o in some_list %} <tr class="{% cycle 'row1' 'row2' %}"> ... </tr> {% endfor %}
extends
extends tag is used for inheritance of templates in django. One needs to repeat the same code again and again. Using extends we can inherit templates as well as variables.
Example Assume the following directory structure:dir1/
In template.html, the following paths would be valid:
template.html
base2.html
my/
base3.html
base1.html{% extends "./base2.html" %}
To check more about extends tag, visit – extends – Django Template Tags
{% extends "../base1.html" %}
{% extends "./my/base3.html" %}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.
Examplehtml {% if athlete_list %} Number of athletes: {{ athlete_list|length }} {% elif athlete_in_locker_room_list %} Athletes should be out of the locker room soon! {% else %} No athletes. {% endif %}
{{ athlete_list|length }}
variable. As one can see, the if tag may take one or several{% elif %}
clauses, as well as an{% else %}
clause that will be displayed if all previous conditions fail. These clauses are optional. To check more about if tag, visit – if – Django Template Tagsfor loop
for tag loops over each item in an array, making the item available in a context variable. Example For example, to display a list of athletes provided in athlete_list:html <ul> {% for athlete in athlete_list %} <li>{{ athlete.name }}</li> {% endfor %} </ul>
for … empty loop
for tag loops over each item in an array, making the item available in a context variable. The for tag can take an optional{% empty %}
clause whose text is displayed if the given array is empty or could not be found. This is basically used as a condition to be followed to check if queryset is empty and what action to be performed in the same scenario.
Examplehtml <ul> {% if athlete_list %} {% for athlete in athlete_list %} <li>{{ athlete.name }}</li> {% endfor %} {% else %} <li>Sorry, no athletes in this list.</li> {% endif %} </ul>
Boolean Operators
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.
Examplehtml <ul> {% if variable boolean_operator value %} // statements {% endif %} </ul>
firstof
firstof tag Outputs 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 %}
This is equivalent to:{% if var1 %}
One can also use a literal string as a fallback value in case all passed variables are False:
{{ var1 }}
{% elif var2 %}
{{ var2 }}
{% elif var3 %}
{{ var3 }}
{% endif %}{% firstof var1 var2 var3 "fallback value" %}
To check more about firstof tag, visit – firstof – Django Template Tagsinclude
include tag loads a template and renders it with the current context. This is a way of “including” other templates within a template. The template name can either be a variable or a hard-coded (quoted) string, in either single or double quotes.
Example{% include "foo/bar.html" %}
Normally the template name is relative to the template loader’s root directory. A string argument may also be a relative path starting with ./ or ../ as described in the extends tag. To check more about include tag, visit – include – Django Template Tagslorem
lorem tag displays random “lorem ipsum” Latin text. This is useful for providing sample data in templates.
Example- {% lorem %} will output the common “lorem ipsum” paragraph.
- {% lorem 3 p %} will output the common “lorem ipsum” paragraph and two random paragraphs each wrapped in HTML tags.
- {% lorem 2 w random %} will output two random Latin words.
now
now tag displays the current date and/or time, using a format according to the given string. Such string can contain format specifiers characters as described in the date filter section.
ExampleIt is {% now "D d M Y" %}
Above tag will display, Tue 04 Feb 2020 To check more about now tag, visit – now – Django Template Tagsurl
url tag 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 'some-url-name' v1 v2 %}
The first argument is a URL pattern name. It can be a quoted literal or any other context variable. Additional arguments are optional and should be space-separated values that will be used as arguments in the URL. To check more about url tag, visit – url – Django Template Tags
Contact Us