jquery - 检索字典值 jinja

标签 jquery python dictionary jinja2

我有一个看起来像这样的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/

相关文章:

javascript - 独立的事件处理程序以实现可重用性

jquery - 如何使用 jQuery 从 html 表格行中的第 5 个单元格获取数据

list - 如何检查列表是否包含Dart中的特定值?

Java map 适配器在 Python 中等效于使用 wsdl

Python 3 - 列表到字典。按单词排序

javascript - 绑定(bind)函数,其中包含 "this"(JQuery)

jquery - 仅将整数放入条形图和折线图的 x 轴和 y 轴 - Flot

python - 在 matplotlib 中绘制多个 y 轴和颜色条

python - 根据另一个文件中保存的列表选择文件中的变量

python - xpath 属性包含特殊字符