Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - How to do tuple unpacking in a template 'for' loop

In my views.py, I'm building a list of two-tuples, where the second item in the tuple is another list, like this:

[ Product_Type_1, [ product_1, product_2 ],   Product_Type_2, [ product_3, product_4 ]] 

In plain old Python, I could iteration the list like this:

for product_type, products in list:     print product_type     for product in products:         print product 

I can't seem to do the same thing in my Django template:

{% for product_type, products in product_list %}     print product_type     {% for product in products %}         print product     {% endfor %} {% endfor %} 

I get this error from Django:

Caught an exception while rendering: zip argument #2 must support iteration

Of course, there is some HTML markup in the template, not print statements. Is tuple unpacking not supported in the Django template language? Or am I going about this the wrong way? All I am trying to do is display a simple hierarchy of objects - there are several product types, each with several products (in models.py, Product has a foreign key to Product_type, a simple one-to-many relationship).

Obviously, I am quite new to Django, so any input would be appreciated.

like image 516
Chris Lawlor Avatar asked Nov 07 '08 02:11

Chris Lawlor


People also ask

Is it possible to unpack tuple items using for loop?

We can do the tuple unpacking right inside the for loop itself because anything you can put on the left-hand side of the equal sign, you can put in between the for and the in in a for loop.

Is unpacking possible in a tuple?

In python tuples can be unpacked using a function in function tuple is passed and in function, values are unpacked into a normal variable. The following code explains how to deal with an arbitrary number of arguments. “*_” is used to specify the arbitrary number of arguments in the tuple.


2 Answers

Another way is as follows.

If one has a list of tuples say:

mylst = [(a, b, c), (x, y, z), (l, m, n)] 

then one can unpack this list in the template file in the following manner. In my case I had a list of tuples which contained the URL, title, and summary of a document.

{% for item in mylst %}          {{ item.0 }} {{ item.1}} {{ item.2 }}     {% endfor %} 
like image 159
Ashwin Rao Avatar answered Sep 18 '22 15:09

Ashwin Rao


it would be best if you construct your data like {note the '(' and ')' can be exchanged for '[' and ']' repectively, one being for tuples, one for lists}

[ (Product_Type_1, ( product_1, product_2 )),    (Product_Type_2, ( product_3, product_4 )) ] 

and have the template do this:

{% for product_type, products in product_type_list %}     {{ product_type }}     {% for product in products %}         {{ product }}     {% endfor %} {% endfor %} 

the way tuples/lists are unpacked in for loops is based on the item returned by the list iterator. each iteration only one item was returned. the first time around the loop, Product_Type_1, the second your list of products...

like image 42
Jake Avatar answered Sep 20 '22 15:09

Jake