【问题标题】:How do I iterate and index a list in Django template如何在 Django 模板中迭代和索引列表
【发布时间】:2021-04-08 01:28:23
【问题描述】:
<!-- we want the first to be visible -->
{% for j in listOfStoresUsed %}
    <div class="col-sm-8" style="display: none; margin-left: 6em;" id = "store-{{j}}">
        {% for i  in itemsOrganizedByStore  %} 
            <img  width="200em" height="200em" src={{i.item.imgURL}}> <p>{{i.item.name}} Q:{{i.quantity}}</p><br>

        {% endfor %}

{% endfor %}            

itemsOrganizedByStore 是一个列表,我想做类似的事情:

for i in itemsOrganizedByStore[x] 

x 是一个将在上面的 for 循环中声明的索引。如何在 Django 中为 x 使用数字?

【问题讨论】:

  • 显示您对此模板的看法,您如何获得listOfStoresUseditemsOrganizedByStore

标签: django


【解决方案1】:

也许你想在模板中获取键和值

{% for key, value in your_dict.items %}

    {{ key }}-{{ value }}

{% endfor %}

您的问题已经在here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 2021-10-28
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 2012-12-24
    相关资源
    最近更新 更多