not sure how to unpack a list within a tuple within another list inside the template

41 views
Skip to first unread message

Mario Gudelj

unread,
Jan 11, 2012, 4:33:48 PM1/11/12
to django...@googlegroups.com
Hi Djangoers,

I have a default dict variable final_d = defaultdict(list) that looks like this:

[(order1, [customer2, customer1]), (order2, [customer3, customer5, customer6]) ]

I've tried everything possible inside the template and I can't unpack this thing. I'm passing final_d to the template inside the dictionary like this: d = {'final_d':final_d}

This is my template which should I think work from what I've read on SO and elsewhere:

<ul class="main-listing">
                                    {% for order, customers in final_d %}

                                        <li>
                                            <h3><a href="{{ order.getabsoluteurl }}">{{ ordder.name }}</a></h3>
                                            <h4>Cusotmers</h4>
                                            <ul class="customers">
                                            {% for customer in customers %}
                                                <li><a href="#">{{ customer.name }}</a></li>
                                            {% endfor %}
                                            </ul>
                                        </li>
                                    {% endfor %}
</ul>

I have tried absolutely everything and I can't get this to render.

The above code doesn't render a result. if I change {% for order, customers in final_d %} to {% for order in final_d %} I do get the order details, but I can't access customer details.

Thank you for your help!

-m

Daniel Roseman

unread,
Jan 12, 2012, 5:47:32 AM1/12/12
to django...@googlegroups.com
A defaultdict operates just like a normal dict in that iterating over it just returns the keys. Usually you would do {% for order, customers in final_d.items %} to iterate over both keys and values.

However I'm confused by your initial description. What you show is not a dict at all, but a list of 2-tuples (each containing a string and a list). Is that a single value of the dict, or what?
--
DR.

Masklinn

unread,
Jan 12, 2012, 5:53:38 AM1/12/12
to django...@googlegroups.com
On 2012-01-11, at 22:33 , Mario Gudelj wrote:
> Hi Djangoers,
>
> I have a default dict variable final_d = defaultdict(list) that looks like
> this:
>
> [(order1, [customer2, customer1]), (order2, [customer3, customer5,
> customer6]) ]
DefaultDicts are dicts, when you iterate over dicts directly you iterate over their keys not pairs of (key, value). So your dict does not look like this, it looks like this:

{order1: [customer2, customer1], order2: [customer3, customer5, customer6]}

and there is no way iterating over it will yield anything but its keys

Either use `dict.iteritems()` to iterate over (key, value) pairs or iterate over it and then dereference the values corresponding to each key.

Masklinn

unread,
Jan 12, 2012, 5:55:38 AM1/12/12
to django...@googlegroups.com
On 2012-01-12, at 11:47 , Daniel Roseman wrote:
> However I'm confused by your initial description. What you show is not a
> dict at all, but a list of 2-tuples (each containing a string and a list).
> Is that a single value of the dict, or what?

I'm guessing it's the initialization vector for the dict, though I'm not
sure why he used this for a non-ordered dict.

>>> dict([(1, [2, 3]), (5, [6, 7, 8])])
{1: [2, 3], 5: [6, 7, 8]}
>>> collections.defaultdict(lambda: [], [(1, [2, 3]), (5, [6, 7, 8])])
defaultdict(<function <lambda> at 0x1004ce488>, {1: [2, 3], 5: [6, 7, 8]})


Bill Freeman

unread,
Jan 12, 2012, 3:46:01 PM1/12/12
to django...@googlegroups.com
Mario,

While there are syntaxes for doing this sort of thing in the template language,
I would expect the template to start looking messy, and be hard to maintain.

May I suggest that you wrap this stuff in a class to provide attribute
like access
to the data that you need? This puts the access logic in python, and makes
the template code more transparent.

Bill

Mario Gudelj

unread,
Jan 12, 2012, 4:17:42 PM1/12/12
to django...@googlegroups.com
Thanks for your help guys. I ended up creating a class for for orders and customers class and then I passed those objects to the tepmlate inside a dict. That worked.

Cheers,



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Reply all
Reply to author
Forward
0 new messages