Hi Todd, On Mon, 2006-04-24 at 20:30 -0400, Todd O'Bryan wrote: > I'm trying to display the values from a form on a summary page using > a template. (Actually, I'm trying to do something more complicated, > but let's start here.) Using {% debug %} I can see that, for example, > > answers = { 'a':1, 'b':2, 'c':3 } > > and in my template I have > > {% for answer in answers.keys %} > {{answer}} = {{answers.answer}}<br/> > {% endfor %} > > I would expect this to print > > a = 1 > b = 2 > c = 3 > > but I get > > a = > b = > c = > > Am I wrong that the dot notation is supposed to do dictionary lookup > or is there some other way to do this?
The problem you are encountering is that Django is looking for the literal key 'answer' in your dictionary. It does not do indirection in the way you are expecting (look at the value of 'answer' and then use that value to do the lookup. To achieve what you are after, you can do this: {% for answer in answers.items %} {{ answer.0 }} = {{ answer.1 }}<br/> {% endfor %} This will give you the answer in arbitrary order, however. If you want to sort things, you can use a slightly undocumented side-effect of the dictsort filter and apply it to the list in the loop head (the undocumented fact is that "|dictsort" works on lists like this, not just dictionaries): {% for answer in answers.items|dictsort:"0" %} {{ answer.0 }} = {{ answer.1 }}<br/> {% endfor %} This will sort on the "key" in the dictionary -- the first argument in each tuple in the list. 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 -~----------~----~----~----~------~----~------~--~---