...because the world doesn't have enough bloggers
Tuesday, October 16. 2007
Iterating over a dictionary in Django templates
Trackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
And I can't find how to get value of dictionary element in first elment of list. All so is complicated :)
I don't usually respond on stuff like this, but this is very helpful. Seems like you hunt all day just to find the simple stuff.
Hmm. I have the same problem. I can't use the main solution here, but can use item.0 and item.1.
I wonder if behavior varies between versions. We are suing 0.96. What are you using David and David?
I wonder if behavior varies between versions. We are suing 0.96. What are you using David and David?
the key.0 solution works for me and i'm using the django template engine that comes with AppEngine sdk (if that helps)
Figured it out, it is the version. Django 0.96 does not support expanding tuples, so there is no real way to use anything other then .0 and .1 for 0.96 users, or to use a different data structure entirely.
For some wierd reason that did not worked for me...
However... this did...
{% for key in dictionary.items %}
{{ key.0 }}
{{ key.1 }}
{% endfor %}
However... this did...
{% for key in dictionary.items %}
{{ key.0 }}
{{ key.1 }}
{% endfor %}
yup i have the same issue. the documented way doesn't work but this does:
{% for key in data.items %}{{ key.0 }}:{{ key.1 }}{% endfor %}
{% for key in data.items %}{{ key.0 }}:{{ key.1 }}{% endfor %}
Hm, I can't seem to get nested dicts to work though... is that supported?
Thanks a million!
This hint not only works like a goddamn charm but also saved me hours!
You rock! :-)
This hint not only works like a goddamn charm but also saved me hours!
You rock! :-)
Nice hint,but is there any way to sort the dictionary based on values or keys and iterate ?
Hi ganesh.
The concept of 'sort' applies only to a collection which has _order_ -- a sequence like a list.
A mapping (e.g. a dictionary) has NO order, thus it cannot be sorted.
Anyway, there are workarounds. Check this:
http://code.activestate.com/recipes/52306/
http://code.activestate.com/recipes/304440/
Hope being helpful!
Matías.-
The concept of 'sort' applies only to a collection which has _order_ -- a sequence like a list.
A mapping (e.g. a dictionary) has NO order, thus it cannot be sorted.
Anyway, there are workarounds. Check this:
http://code.activestate.com/recipes/52306/
http://code.activestate.com/recipes/304440/
Hope being helpful!
Matías.-
oh yeah thanks !
Why can'"t I read this in django doc ???
in others words where is this in the doc ?
Why can'"t I read this in django doc ???
in others words where is this in the doc ?
To access JSON data:
{% for key,value in json_.items %}{{value.x.y.z}}{% endfor %}
acccess z inside y inside x in each value dictionary.
{% for key,value in json_.items %}{{value.x.y.z}}{% endfor %}
acccess z inside y inside x in each value dictionary.
Remember to not pass you dictionary directly to the template btw.
Example:
animals = {'cow':'moo', 'ape':'eek', 'sheep':'bleeeh' }
#BAD!
return render_to_response('temp.htm', animals)
#GOOD
return render_to_response('temp.htm', { 'animals' : animals } )
Example:
animals = {'cow':'moo', 'ape':'eek', 'sheep':'bleeeh' }
#BAD!
return render_to_response('temp.htm', animals)
#GOOD
return render_to_response('temp.htm', { 'animals' : animals } )