【发布时间】:2015-07-09 03:59:25
【问题描述】:
我知道使用“。”访问模板中的字典项的符号(就像this post 解释的那样),但是当我尝试访问特定键时,它不起作用。怎么了?
我的字典:
images[mykey] = "the value string"
然后在模板中,我尝试按键访问,但不起作用:
{% for x in otherdict %}
{{ x }}
{{ images.items.x }}
{% endfor %}
我可以通过images dict 循环就好了:
{% for k,v in images.items %}
{{ k }} -- {{ v }}
{% endfor %}
但我需要通过特定的键访问!
【问题讨论】:
-
您的字典没有名为“x”的元素。
-
"x" 是 dict 的一个键......因为它是一个变量,我该如何让它工作?
标签: python django django-templates