Re: not sure how to unpack a list within a tuple within another list inside the template
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, On 12 January 2012 21:55, Masklinn wrote: > 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( at 0x1004ce488>, {1: [2, 3], 5: [6, 7, > 8]}) > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: not sure how to unpack a list within a tuple within another list inside the template
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 -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: not sure how to unpack a list within a tuple within another list inside the template
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( at 0x1004ce488>, {1: [2, 3], 5: [6, 7, 8]}) -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: not sure how to unpack a list within a tuple within another list inside the template
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. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: not sure how to unpack a list within a tuple within another list inside the template
On Wednesday, 11 January 2012 21:33:48 UTC, somecallitblues wrote: > > 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: > > > {% for order, customers in final_d %} > > > {{ ordder.name }} > Cusotmers > > {% for customer in customers %} > {{ > customer.name }} > {% endfor %} > > > {% endfor %} > > > 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 > 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. -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/WIowdkQrZGoJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.