【问题标题】:Retrieve dictionary value jinja检索字典值 jinja
【发布时间】:2016-01-24 18:06:31
【问题描述】:

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

        });

它没有被定义,也没有打印警报。

【问题讨论】:

    标签: jquery python dictionary jinja2


    【解决方案1】:

    你需要对字典使用像python这样的调用(它不是集合):

    {% for i in dict %}
        {{ i['hat'] }}
    {% endfor %}
    

    集合可以作为字典访问,而字典不能作为集合调用。任何你需要的方式,如果这个集合使用 i.hat,或者如果它是一个集合或字典,则使用 i['hats']。

    尝试替换它:

    alert( {{ hat }} );
    

    到:

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

    【讨论】:

    • 抱歉,是合集,忘记放OP了,检查编辑。
    • 很高兴能帮上忙
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多