# looping through a list of QuerySets in Template

**URL:** <https://forum.djangoproject.com/t/looping-through-a-list-of-querysets-in-template/24753>\
**Category:** Using the ORM\
**Created:** [October 24, 2023, 7:31am UTC](https://forum.djangoproject.com/t/looping-through-a-list-of-querysets-in-template/24753 "2023-10-24T07:31:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dariofiru](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dariofiru/32/16608_2.png) [@dariofiru](https://forum.djangoproject.com/u/dariofiru)\
**Post date:** [October 24, 2023, 7:31am UTC](https://forum.djangoproject.com/t/looping-through-a-list-of-querysets-in-template/24753/1 "2023-10-24T07:31:10Z")

</div>

Hi all, I’m fairly new to Django so apologies if what I’m trying to do makes little sense. Anyhow:

I’m extracting some products and for each product I have a bunch of orders (I don’t know how many)  
to extract. Everything is then passed to a template, but I’m unable to loop through the sales.

Here’s the code from my view and my template, I think the issue is that I’m passing a list of QuerySets to my html template and I don’t know how to loop and access the various fields.

```
    product_list = Product.objects.filter(product_id=id)
           sales_data = [] #square brackets
           for product in product_list:
                if Sales.objects.filter(product_sold=product):
                sales_data.append(Sales.objects.filter(product_sold=product).values()) 

       return render(request, "sales/index.html", {
            "product_list": product_list, "sales_data ": sales_data 
        })

```

in my html template:

{% for product in product\_list %}

#### {{product.product\_name}}

{% for sale in sales\_data %}

???

{% endfor %}

{% endfor %}

---

<div class="post-metadata">

**Author:** ![StephanieAG](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/stephanieag/32/17567_2.png) [@StephanieAG](https://forum.djangoproject.com/u/StephanieAG)\
**Post date:** [October 24, 2023, 10:28am UTC](https://forum.djangoproject.com/t/looping-through-a-list-of-querysets-in-template/24753/2 "2023-10-24T10:28:35Z")

</div>

I’m a beginner too, so please keep that in mind while I take a shot at helping. 😁

> [@dariofiru](#):
>
> I think the issue is that I’m passing a list of QuerySets to my html template and I don’t know how to loop and access the various fields.

I had a similar confusion recently — my notes in the sixth reply of this post (or the whole post) might be helpful: [Context data and related\_name (followers list) - #6 by StephanieAG](https://forum.djangoproject.com/t/context-data-and-related-name-followers-list/21733/6)

I’m not sure if this is “the” answer, but maybe it’s something to do with having the .all Django queryset function added in your template like this:

```auto
{% for product in product_list.all %}
...
{% for sale in sales_data.all %}
...

```

From your post, I’m unsure if you were saying just the `sales_data` for loop information isn’t showing on the template or both the `product_list` and the `sales_data` for loops weren’t coming through. Are they both not working or just the `sales_data` one?

---

<div class="post-metadata">

**Author:** ![dariofiru](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dariofiru/32/16608_2.png) [@dariofiru](https://forum.djangoproject.com/u/dariofiru)\
**Post date:** [October 24, 2023, 10:59am UTC](https://forum.djangoproject.com/t/looping-through-a-list-of-querysets-in-template/24753/3 "2023-10-24T10:59:44Z")

</div>

it’s just the sales\_data not working, I can see that the data is transferred to the template but I can’t find a way to work with it.

Basically I can see that is a QuerySet inside a list, so I can access single records with {{sale[0]}} but can’t loop or access specific fields.

I’ll have a look at your post, thanks for sharing

---

<div class="post-metadata">

**Author:** ![onyeibo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/onyeibo/32/11494_2.png) [@onyeibo](https://forum.djangoproject.com/u/onyeibo)\
**Post date:** [October 25, 2023, 12:08am UTC](https://forum.djangoproject.com/t/looping-through-a-list-of-querysets-in-template/24753/4 "2023-10-25T00:08:13Z")

</div>

> [@dariofiru](#):
>
> {% for product in product\_list %}
> 
> #### {{product.product\_name}}
> 
> {% for sale in sales\_data %}
> 
> ???
> 
> {% endfor %}
> 
> {% endfor %}

**Try this instead:**

```auto
{% for product in product_list %}
    {{ product.product_name }}
    {% for sale in sales_data %}
        {% for item in sale %}
            {{ item.keyword }}
            {{ item.another_keyword }}
        {% endfor %}
    {% endfor %}
{% endfor %}

```

Sale is actually a queryset (iterable)
