检索字典值 jinja

Retrieve dictionary value jinja

我有一个 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['hats'] 如果它是一个集合或字典,您需要使用任何方式 i.hat

试试替换一下:

alert( {{ hat }} );

至:

alert( {{ i['hat'] }} );