Hi Bill, On Thu, 2006-03-02 at 23:29 +0000, Bill de hÓra wrote: > > I'm trying to understand how dictionary lookups work in templates. To > that end I'm passing in a dictionary of lists like this, > > allyears = {"2002":["a","b","c"], "2003":["a","b","c"]} > return render_to_response('press/inthepress_index', > "allyears":allyears,) > > into the template, ie in the webpage, show all thingies for each year. > > I have a corresponding fragment like this in a template: > > [[[ > {% for yearkey in allyears.keys %} > <h3>{{yearkey}}</h3> > <ul> > {% for entry in allyears.yearkey %} > <li>{{entry}}</li> > {% endfor %} > </ul> > {% endfor %} > ]]] [...snip...]
So reorganising the data structure as nested lists has already been suggested, but I suspect if you have dictionaries then you want to use dictionaries (I would). Although there is no natural way to do what you want, that I know of, here is one solution that works {% for data in data.items %} <h3>{{data.0}}</h3> <ul> {% for entry in data.1 %} <li>{{entry}}</li> {% endfor %} </ul> {% endfor %} Just using the fact that you can refer to the list items by position: data.0 and data.1 are converted to data[0] and data[1] in Python terms and so iterate over the keys and values respectively. Cheers, Malcolm --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---