Render dictionary keys and values in a template?

1,213 views
Skip to first unread message

Eric Walstad

unread,
May 4, 2006, 10:45:53 PM5/4/06
to Django users
Is it possible to iterate over a dictionary in a django template, displaying
the dictionary values, without knowing the dictionary's keys beforehand?

I want to simply list keys and values of a dictionary passed into a template,
but I haven't been able to figure out how.

I'd like to do something like:
{% for err in errors %}
{{ errors.err }}
{ % end for %}

In pure python I could do:
plotz = {'foo':'bar', 'fizz':'bang'}
for frob in plotz:
print frob

foo
fizz

I've been able to do this in the template, but I can't get the following to
work in a template:

for frob in plotz:
print plotz[frob]

bar
bang

Can I do this in a Django template?

Thanks in advance,

Eric.

Sam Tran

unread,
May 4, 2006, 11:03:11 PM5/4/06
to django...@googlegroups.com

{% for i in plotz.items %}
Key {{ i.0 }}
Value {{ i.1 }}
{% endfor %}

Reply all
Reply to author
Forward
0 new messages