迭代Django模板中的列表列表

时间:2015-07-25 18:34:08

标签: python django django-templates jinja2

我有一个上下文变量:

"images": [
        {
            "date": "2015-06-02T11:51:53Z",
            "image": "auction/Screenshot_from_2015-05-18_235741.png",
            "auction_id": 1,
            "id": 1
        },
        {
            "date": "2015-06-23T09:38:45Z",
            "image": "auction/Screenshot_from_2015-06-22_203407.png",
            "auction_id": 1,
            "id": 2
        },
        .....
    ],

我想以下列方式遍历这个字典列表

{% for image in images %}
    <a class="thumb-item-link" data-slide-index="{{over here should be the index of the current iteration}}" href=""><img src="/media/{{image.image}}" alt="img"/></a> 
{% endfor %}

例如: -

<a class="thumb-item-link" data-slide-index="0" href=""><img src="/media/auction/Screenshot_from_2015-05-18_235741.png" alt="img"/></a> 

<a class="thumb-item-link" data-slide-index="1" href=""><img src="/media/auction/Screenshot_from_2015-06-22_203407.png" alt="img"/></a> 

<a class="thumb-item-link" data-slide-index="2" href=""><img src="/media/auction/Screenshot_from_2015-06-22_203488.png" alt="img"/></a> 

此处data-slide-index正在根据当前迭代更改其值。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:4)

您可以使用此link

{{forloop.counter0}}
每个循环中的

和django将枚举0的索引。

相关问题