Will Curran
Will Curran

Reputation: 7110

Django templates: how do I use key to access value?

I don't want to iterate through the dictionary. I have a key, and simply want to return the value for that key if it exists.

I'm not getting any results.

users // a dictionary of user_ids and values
user.key // a user id.

{{ users.user.key }}

This displays nothing when I know the value for the key passed exists.

Upvotes: 1

Views: 1038

Answers (1)

vartec
vartec

Reputation: 134721

The problem is that Django interprets users.user.key as users.user[key], which of course is not what you want.

You can use with directive to work around this.

 {% with user.key as user_key %}
    {{users.user_key}} 
 {% endwith %}

Upvotes: 4

Related Questions