HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
A for loop is used for iterating over a sequence, like looping over items in an array, a list, or a dictionary
A for
loop is used for iterating over a sequence, like looping over items in an
array, a list, or a dictionary.
Loop through the items of a list:
{% for x in fruits %}
<h1>{{ x }}</h1>
{% endfor %}
Loop through a list of dictionaries:
{% for x in cars %}
<h1>{{ x.brand }}</h1>
<p>{{ x.model }}</p>
<p>{{ x.year }}</p>
{% endfor %}
Data in a model is like a table with rows and columns.
The Members model we created earlier has five rows, and each row has three columns:
id | firstname | lastname |
---|---|---|
1 | Emil | Refsnes |
2 | Tobias | Refsnes |
3 | Linus | Refsnes |
4 | Lene | Refsnes |
5 | Stalikken | Refsnes |
When we fetch data from the model, it comes as a QuerySet object, with a similar format as the cars example above: a list with dictionaries:
<QuerySet [
{
'id': 1,
'firstname': 'Emil',
'lastname': 'Refsnes'
},
{
'id': 2,
'firstname': 'Tobias',
'lastname': 'Refsnes'
},
{
'id': 3,
'firstname': 'Linus',
'lastname': 'Refsnes'
},
{
'id': 4,
'firstname': 'Lene',
'lastname': 'Refsnes'
},
{
'id': 5,
'firstname': 'Stalikken',
'lastname': 'Refsnes'
}
]>
Loop through items fetched from a database:
{% for x in members %}
<h1>{{ x.id }}</h1>
<p>
{{ x.firstname }}
{{ x.lastname }}
</p>
{% endfor %}
The reversed
keyword is used when you want
to do the loop in reversed order.
{% for x in members reversed %}
<h1>{{ x.id }}</h1>
<p>
{{ x.firstname }}
{{ x.lastname }}
</p>
{% endfor %}
The empty
keyword can be used if you want to
do something special if the object is empty.
<ul>
{% for x in emptytestobject %}
<li>{{ x.firstname }}</li>
{% empty %}
<li>No members</li>
{% endfor %}
</ul>
The empty
keyword can also be used if the object does not exist:
<ul>
{% for x in myobject %}
<li>{{ x.firstname }}</li>
{% empty %}
<li>No members</li>
{% endfor %}
</ul>
Django has some variables that are available for you inside a loop:
The current iteration, starting at 1.
<ul>
{% for x in fruits %}
<li>{{ forloop.counter }}</li>
{% endfor %}
</ul>
The current iteration, starting at 0.
<ul>
{% for x in fruits %}
<li>{{ forloop.counter0 }}</li>
{% endfor %}
</ul>
Allows you to test if the loop is on its first iteration.
Draw a blue background for the first iteration of the loop:
<ul>
{% for x in fruits %}
<li
{% if forloop.first %}
style='background-color:lightblue;'
{% endif %}
>{{ x }}</li>
{% endfor %}
</ul>
Allows you to test if the loop is on its last iteration.
Draw a blue background for the last iteration of the loop:
<ul>
{% for x in fruits %}
<li
{% if forloop.last %}
style='background-color:lightblue;'
{% endif %}
>{{ x }}</li>
{% endfor %}
</ul>
The current iteration if you start at the end and count backwards, ending up at 1.
<ul>
{% for x in fruits %}
<li>{{ forloop.revcounter }}</li>
{% endfor %}
</ul>
The current iteration if you start at the end and count backwards, ending up at 0.
<ul>
{% for x in fruits %}
<li>{{ forloop.revcounter0 }}</li>
{% endfor %}
</ul>