iterate through list in django template

How to Loop Through List in Django Template

Sometimes you may need to iterate through a list or use a for loop in Django template to loop through list items. In this article, we will look at different examples to help you loop through list in Django template.


How to Loop Through List in Django Template

Here are the steps to loop through list in Django template.


1. Loop through single list

Let us say you have a Django view that returns 1 list to your template.

def test(request):
    ID = [1,2]
    return render(request, 'page.html',{'ID':ID})

Here is a simple way to loop through it in your Django template page.html

{% for i in ID %}
   <p>ID: {{ i }}</p>
{% endfor %}

In the above code, we loop through the ID list returned by our Django view. You can refer to each item directly within {{ }}.


2. Loop through multiple lists

If you want to send multiple lists to Django template, it is advisable to use zip function and convert it into a tuple of lists before sending them in your response. Here is an example.

def test(request):
    ID = [1,2]
    name = ['Jim','Jane']
    age = [35,25]
    employees=zip(ID, name, age)
    return render(request, 'page.html',{'employees':employees})

zip function above packs all the lists into a single list of tuples (ID, name, age). You can loop through all your lists at once by simply looping through employees list, as shown below.

{% for employee in employees %}
   <tr>
                <td>ID: {{ employee.0 }}</td>
                <td>Name: {{ employee.1 }}</td>
                <td>Age: {{ employee.2 }} </td>
            </tr>
{% endfor %}

In the above for loop, you can directly access individual list items of each iteration using indexes 0,1,2…0 is the index for the first item of each tuple in the list. In our case, ID, name, age have indexes 0,1, 2 respectively.


3. Iterate list of dictionaries

If you send a list of dictionaries in response, then you can easily refer to each item using its key. Here is the above example presented using a list of dictionaries. Instead of creating separate lists for ID, name & age, let us say you have a single list of dictionaries as shown below.

def test(request):
    employees = [
          {'ID':1,'name':'Jim','age':35},
          {'ID':2,'name':'Jane','age':25},
    ]
    return render(request, 'page.html',{'employees':employees})

In this case, you can directly access each list’s item using their key names instead of using indexes as shown below.

{% for employee in employees %}
   <tr>
                <td>ID: {{ employee.id }}</td>
                <td>Name: {{ employee.name }}</td>
                <td>Age: {{ employee.age }} </td>
            </tr>
{% endfor %}

In this article, we have learnt three different ways to iterate over list in Django template. When you are sending multiple lists to your view’s response, the key is to identify the right data structure that you want to pass from your view to the template. This will make it super easy to iterate over lists. Otherwise, you might end up looping multiple times.

Also read:

How to Run Python Script in Django Shell
How to Disable CSRF Validation in Django View
How to Enable CORS in Django Project
How to Combine Querysets in Django
How to Fix NoReverseMatch Error in Django