Tags and Loops
Let’s learn about the functionality of tags in templates and how to create loops in templates.
We'll cover the following
Introduction
A template also contains tags that are used to control the logic of the template. These can be conditions, loops, or filters. Let’s take a look at each of them one by one.
Syntax of tags
Tags look like this: {% tag %}
. Tags are more complex than variables: some create text in the output, some control the flow of the template using for
loops or if-else
, and some load external information into the template which is used by later variables.
Some tags require beginning and ending tags:
{% tag %}
… tag contents …{% endtag %}
.
Django ships with about two dozen built-in template tags. We will be discussing a few of them below:
Get hands-on with 1400+ tech skills courses.