for loop - Django Template Tags
Last Updated :
17 May, 2025
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 without needing to write complex logic in the view.
Syntax of the for Tag
{% for item in iterable %}
<!-- Do something with 'item' -->
{% endfor %}
- iterable: This is the collection (such as a list or a queryset) over which the loop will iterate.
- item: This represents each individual element in the iterable during the iteration.
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>
Illustration of How to use "for tag" in Django templates using an example. Consider a project named "geeksforgeeks" having an app named geeks.
Refer to the following articles to check how to create a project and an app in Django.
View (geeks/views.py)
Python
# import Http Response from django
from django.shortcuts import render
# create a function
def geeks_view(request):
# create a dictionary
context = {
"data" : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
}
# return response
return render(request, "geeks.html", context)
URL Configuration (geeks/urls.py)
Python
from django.urls import path
# importing views from views.py
from .views import geeks_view
urlpatterns = [
path('', geeks_view),
]
Template (templates/geeks.html)
HTML
{% for i in data %}
<div class="row">
{{ i }}
</div>
{% endfor %}
Output
for loop - Django TemplateAdvanced Usage
One can loop over a list in reverse by using {% for obj in list reversed %}.
If you need to loop over a list of lists, you can unpack the values in each sublist into individual variables. For example, if your context contains a list of (x, y) coordinates called points, you could use the following to output the list of points:
HTML
{% for x, y in points %}
There is a point at {{ x }}, {{ y }}
{% endfor %}
Example:
If your context is:
points = [(1, 2), (3, 4), (5, 6)]
Output:
There is a point at 1, 2
There is a point at 3, 4
There is a point at 5, 6
This can also be useful if you need to access the items in a dictionary. For example, if your context contained a dictionary data, the following would display the keys and values of the dictionary:
HTML
{% for key, value in data.items %}
{{ key }}: {{ value }}
{% endfor %}
Example:
If your context is:
data = {'name': 'Alice', 'age': 30, 'city': 'Paris'}
Output:
name: Alice
age: 30
city: Paris
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