24 de septiembre de 2009

Iterating over a dictionary in Django templates

Just a quick one: To iterate over a list you do something along the lines of:

{% for item in list%}{{ item }}{% endfor %}

But this fails (silently, of course) for dictionaries and the magic you are looking for is to call .items on the dictionary then iterate key,value across that:
{% for key,value in dictionary.items %}{{ value }}{% endfor %}

Edit: This is now covered in the django documentation itself.

Artículo original: acá.

No hay comentarios: