views:

131

answers:

2

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!

A: 

See these answers.

Hank Gay
Gotta love django templates...that's such a hacked solution, but oh well. Thanks for the response.
Jordan Messina
+1  A: 

A workaround could be using the keys from number_devices and check in the for loop if it is equal to the key provided by number_devices.

{% for key in number_devices.keys %}
    {% for implementation in implementations %}
        {% ifequal key implementation.id %} you got it {% endifequal %}
    {% endfor %}
{% endfor %}

Seems a bit ugly, but should work.

zovision