On 18 déc, 18:31, marco ghidinelli <marc...@gmail.com> wrote: > hello. > > Does django template engine supports nested dictionary?
Yes, indeed. > i'm trying to display a nested array with no luck. > > that's my tries: > > ------------------- python manage.py shell ------------------- > from django.template import Template, Context > list_parts = { > 'root': {'value': [{'tag': 'a'},{'tag':'b'}]}, > 'wheel': {'value': [{'tag': 'c'}, ]} > > } Why do you name it "list_" when it's a dict ? > t = Template(""" > {% for part in list_parts %} This will iterate over the keys. If you want key:values pairs, you have to spell it: {% for partname, partvalue in list_parts.items %} Also, remember that Python dicts are _not_ ordered. If order matters, use a list of (name, value) tuples instead. > <fieldset> > <legend>{{part}}</legend> > <ul> > {% for subpart in part %} Here, "part" is the key (a string). > <li>{{ subpart }}</li> > {% endfor %} > </ul> > </fieldset> > {% endfor %} > """) > > c=Context({'list_parts':list_parts}) > print t.render(c) > -------------------------------------------------------------- > > i tried every possible combination of > > {% for subpart in part %} > {% for subpart in part.part %} > {% for subpart in list_parts.part %} > > but i'm not able to show the information. > > where i am wrong? google for "programming by accident" (a well known antipattern FWIW). > /me hopeless. Here's a working version, with a simpler data structure: from django.template import Template, Context list_parts = [ ('root', [{'tag': 'a'},{'tag':'b'}]), ('wheel', [{'tag': 'c'}, ]), ] t = Template(""" {% for partname, tags in list_parts %} <fieldset> <legend>{{partname}}</legend> <ul> {% for tag in tags %} <li>{{ tag.tag }}</li> {% endfor %} </ul> </fieldset> {% endfor %} """) c=Context({'list_parts':list_parts}) print t.render(c) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---