开发者

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

开发者 https://www.devze.com 2023-02-04 12:43 出处:网络
开发者_如何学JAVAI 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.
开发者_如何学JAVA

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.


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 %}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号