【问题标题】:Dictionary Index of dictionary contained in a list in Django templateDjango模板列表中包含的字典的字典索引
【发布时间】:2017-12-01 16:51:18
【问题描述】:

我正在尝试将列表中包含的所有字典打印到 Django 模板中的列表中。 它的 JSON 格式来自 [https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation?stopid=184&format=json] 我有

<ul>
{% for key, value in results.0.items %}
<li>{{key}} {{value}}</li>
{% endfor %}
</ul>

在我的模板中输出列表中位置 0 处的第一个字典。列表中的字典数量一直在变化,所以我希望根据列表中的数量将它们全部打印出来。

有什么帮助吗?

【问题讨论】:

    标签: python html django django-templates


    【解决方案1】:

    听起来您需要添加另一个循环{% for result in results %}。然后就可以在内循环中循环遍历result.items

    <ul>
      {% for result in results %}
        {% for key, value in result.items %}
          <li>{{key}} {{value}}</li>
        {% endfor %}
      {% endfor %}
    </ul>
    

    【讨论】:

    • 谢谢,我只需要不同列表中的每本词典。
    • 如果您希望每个字典位于不同的列表中,请交换ul 和外部for 标签。
    猜你喜欢
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 2018-08-06
    • 2017-10-24
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 2019-01-11
    相关资源
    最近更新 更多