Skip to content
Advertisement

Django: Nested Loop In Template

JavaScript

In views.py above this loop creates a list of objects per each category. In the template below this loop prints a filtered object list per every item in the category list.

JavaScript

The only thing I am missing is I do not now how to reference the category in the template. I pass the whole list in context, but {{category{{forloop.counter}}}} is not a valid statement.

I would either like to use zip(category,setofinventories) to pass these two items together, or create a category model, filter by that model and then I can reference that model by item?

If I zip these items dictofstuff = [zip(setofinventories,categories)] How do I reference the category in the template?

JavaScript

Returns Error: “Need 2 values to unpack in for loop: got 1.”

Advertisement

Answer

in views i zipped this to a *listofstuff. Forgive the nomenclature. dictofstuff = [zip(categories,setofinventories)]

in template. Again, forgive bad nomenclature. I should go back and rename these but it works.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement