Django dictionary in templates: Grab key from another objects attribute
Posted
by Jordan Messina
on Stack Overflow
See other posts from Stack Overflow
or by Jordan Messina
Published on 2010-06-09T20:39:48Z
Indexed on
2010/06/09
20:42 UTC
Read the original article
Hit count: 241
I have a dictionary called number_devices
I'm passing to a template, the dictionary keys are the ids of a list of objects I'm also passing to the template (called implementations
). I'm iterating over the list of objects and then trying to use the object.id to get a value out of the dict like so:
{% for implementation in implementations %}
{{ number_devices.implementation.id }}
{% endfor %}
Unfortunately number_devices.implementation
is evaluated first, then the result.id
is evaluated obviously returning and displaying nothing. I can't use parentheses like:
{{ number_devices.(implementation.id) }}
because I get a parse error. How do I get around this annoyance in Django templates?
Thanks for any help!
© Stack Overflow or respective owner