python - Django 模板 : how do I use key to access value?

标签 python django templates dictionary

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

我没有得到任何结果。

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

{{ users.user.key }}

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

最佳答案

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

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

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

关于python - Django 模板 : how do I use key to access value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4649747/

相关文章:

python - 在绘图外显示一行文本

python - Django > 表单 > is_unique()

python - 从脚本运行蜘蛛时获取 scrapycrawl 命令的功能

c++ - Boost图递归模板问题

python - 带有 Bokeh 图的 ImportError : No module named _ctypes. Google 应用引擎

python - 在 matplotlib 中显示次要刻度标签时隐藏主要刻度标签

python - 为什么 "docker-compose run"可以在容器外创建文件?

django 1.3 国际化...切换语言需要重新启动服务器?

c++ - 我可以有一个带有可变对象的模板吗?

c++ - 在结构中使用模板