Django templates provide a powerful set of tools for displaying data on a webpage. Like if statements if you want to display data based on certain conditions. In this post, we are going to learn about the Django template for-loop. The for loop allows you to iterate over a list or queryset of data and display it on the page. This can be incredibly useful for displaying lists of items, such as products in an e-commerce store or articles on a blog.
Basic for-loop syntax
Django offers the {% for %}
and {% endfor %}
tags to represent a for loop. The former initializes the loop and it’s where you declare the iteration statement, while the latter is used to indicate the end of the for loop.
Below is the basic syntax of a for loop in a Django template:
{% for item in items %}
{{ item }}
{% endfor %}
In the above example, the for loop is iterating over a list or queryset of items which, in programming, is called the iterable object. The variable “item” is called the iterator variable and is used to represent each individual item in the list. Inside the for loop (between the {% for %}
and {% endfor %}
tags) you can display the contents of the item using double curly braces ({{ }}
).
Iterating Over a List or Queryset
We usually use a for loop in a Django template to iterate over a list or queryset passed from the views. So let’s use that scenario as our example:
Below is the view:
from django.shortcuts import render
from .models import Post
def my_view(request):
posts = Post.objects.all()
return render(request, 'post_detail.html', {'posts': posts})
When we create a view, we have to pass data to the template using the context object. In the case above, ‘posts’ is the variable that holds all the Post objects that have been queried from the models.
{% for post in posts %}
<h2>{{ post.title }}</h2>
<p>Written by {{ post.author }}</p>
<p>{{ post.body|linebreaksbr }}</p>
{% endfor %}
In the template, we then use a for loop to iterate over the posts queryset, and on each iteration we displays the title, author, and body of the current post using the template tags {{ }}
.
Using for loop variables
Django’s template system also provides several variables that you can use inside a for loop to add additional functionality. Some of those commonly used for loop variables include:
- forloop.counter: provides the current iteration number, starting from 1
- forloop.first: True if this is the first iteration of the loop
- forloop.last: True if this is the last iteration of the loop
For example, to display the iteration number for each item in a list, you could do the following:
{% for item in items %}
{{ forloop.counter }}. {{ item }}
{% endfor %}
This would display the number of the item in the list and the content of the item next to the number.
Nested for loops
It’s also possible to use nested for loops in a Django template.
For example, you could use a nested for loop to display a table of items like below:
<table>
{% for row in rows %}
<tr>
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</table
In this example, the outer for loop is iterating over a list of rows, and the inner for loop is iterating over the cells in each row.
Conclusion
This article explored the for loop, one of the most powerful tools for presenting data visually, inside Django templates. We have looked at how to iterate over lists and querysets, how to add more functionality by utilizing the variables, and last but not least, how to utilize nested for loops to display more complex data. Using for loops, you can make your web pages more dynamic and engaging to your users.