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
})
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:
{% 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?
{% 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 %}