我有一个看起来像这样的Python字典
[{"hat": 91, "d1b": 2, "d1c": 1, "d1d": 5, "d1e": 7, "d1f": 77, "d1e": 999}
{"hat": 1, "d2b": 2, "d2c": 3, "d2d": 4, "d2e": 5, "d2f": 6, "d2e": 7}
{"hat": 1, "d3b": 2, "d3c": 3, "d3d": 4, "d3e": 5, "d3f": 6, "d3e": 7}]
我将其作为字典对象(mydict)从 python 传递到 jinja
我想做的是循环遍历每个字典并打印出我搜索的键的值。并将其显示在 jquery 警报框中。
$(document).ready(function() {
{% for i in mydict %}
{{ loop.index0 }}, {{a,["hat"] }}
alert( {{ hat }} );
{% endfor %}
});
当我访问我的网页时,出现错误
Uncaught SyntaxError: Unexpected token &
$(document).ready(function() {
0, (Undefined, [[hat[])
alert( );
1, (Undefined, [hat])
alert( );
2, (Undefined, [hat])
alert( );
});
它没有被定义,也没有打印警报。
最佳答案
您需要使用像Python这样的字典调用(它不是集合):
{% for i in dict %}
{{ i['hat'] }}
{% endfor %}
集合可以作为字典来访问,但字典不能作为集合来调用。如果是集合,则需要使用 i.hat;如果是集合或字典,则需要使用 i['hats']。
尝试更换它:
alert( {{ hat }} );
至:
alert( {{ i['hat'] }} );
关于jquery - 检索字典值 jinja,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34979422/