【问题标题】:Django templates: how do I use key to access value?Django 模板:如何使用键来访问值?
【发布时间】:2011-01-10 17:48:05
【问题描述】:

我不想遍历字典。我有一个键,并且只想返回该键的值(如果存在)。

我没有得到任何结果。

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

{{ users.user.key }}

当我知道传递的键的值存在时,这不会显示任何内容。

【问题讨论】:

    标签: python django templates dictionary


    【解决方案1】:

    问题是Django将users.user.key解释为users.user[key],这当然不是你想要的。

    您可以使用with 指令来解决此问题。

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

    【讨论】:

    • 我在 GAE 上,我认为它有 Django 0.96,但这不起作用。也许我需要创建一个自定义过滤器。
    • 对于 Django 1.4.x 来说,这实际上看起来并不适用。有人告诉我,这不受支持。如果这可行,则必须需要一些未在此答案中列出的其他过滤器。
    猜你喜欢
    • 2012-06-07
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 2014-02-01
    • 2012-06-29
    • 2020-09-02
    • 2011-07-11
    相关资源
    最近更新 更多