Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Nov. 9, 2005 21:39:14

s.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Dictionary keys in templates


Does anyone know of a way to use a variable as a dictionary key within
a template?

I want to be able to do something like this:

{% for key in dictionary.keys %}
... be able to use {{ key }} and "{{ dictionary.key }}" ...
{% endfor %}

dictionary.key assumes the string 'key' is the actual value to lookup
(using dictionary, attribute, method, and list priority). In this
case, the value for key is never resolved.

I have tried a number of combinations to get this to work. Has anyone
been able to do this successfully? I have a number of work arounds in
my code for this, but it is forcing the template process to be much
more specific to the view.


Thanks,

Sarah

Offline

#2 Nov. 9, 2005 22:37:52

K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Dictionary keys in templates


Is there a reason you can't do this:

{% for items in dictionary.items %}
... be able to use {{ items.0 }} and "{{ items.1 }}"...
{% endfor %}

this is equivalent to the python:
for key, value in dictionary.items()

but since for loops in the template language can only create one
variable, you need ot use tuple indexes...

Offline

#3 Nov. 9, 2005 22:44:03

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Dictionary keys in templates


On 11/9/05, Kevin <> wrote:
> Is there a reason you can't do this:
>
> {% for items in dictionary.items %}
> ... be able to use {{ items.0 }} and "{{ items.1 }}"...
> {% endfor %}
>
> this is equivalent to the python:
> for key, value in dictionary.items()

That should work, exactly as stated.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

Board footer

Moderator control

Enjoy the 18th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support