如何在Django模板中运行for循环?
我想运行for循环来显示所有图像,但问题是我必须更改每个图像的“id”(pic-1->pic-2…等等),我知道如何在普通Python中执行此操作,但我不知道如何在Django模板中执行此操作如何在Django模板中运行for循环?,django,Django,我想运行for循环来显示所有图像,但问题是我必须更改每个图像的“id”(pic-1->pic-2…等等),我知道如何在普通Python中执行此操作,但我不知道如何在Django模板中执行此操作 <div class="preview-pic tab-content"> <div class="tab-pane active" id="pic-1"><img src="{{product.image.url}}"></div>
<div class="preview-pic tab-content">
<div class="tab-pane active" id="pic-1"><img src="{{product.image.url}}"></div>
{% for image in image_list %}
<div class="tab-pane" id="pic-2"><img src="http://placekitten.com/400/252" /></div>
<div class="tab-pane" id="pic-3"><img src="http://placekitten.com/400/252" /></div>
<div class="tab-pane" id="pic-4"><img src="http://placekitten.com/400/252" /></div>
<div class="tab-pane" id="pic-5"><img src="http://placekitten.com/400/252" /></div>
{% endfor %}
</div>
<ul class="preview-thumbnail nav nav-tabs">
<li class="active"><a data-target="#pic-1" data-toggle="tab"><img src="http://placekitten.com/200/126" /></a></li>
{% for image in image_list %}
<li><a data-target="#pic-2" data-toggle="tab"><img src="http://placekitten.com/200/126" /></a></li>
<li><a data-target="#pic-3" data-toggle="tab"><img src="http://placekitten.com/200/126" /></a></li>
<li><a data-target="#pic-4" data-toggle="tab"><img src="http://placekitten.com/200/126" /></a></li>
<li><a data-target="#pic-5" data-toggle="tab"><img src="http://placekitten.com/200/126" /></a></li>
{% endfor %}
</ul>
</div>
{图像列表%中图像的百分比}
{%endfor%}
{图像列表%中图像的百分比}
{%endfor%}
您可以使用获取循环索引:
{% for image in image_list %}
<div class="tab-pane" id="pic-{{ forloop.counter }}"><img src={{ image.url }} />
{% endfor %}
您可以使用
{{forloop.counter}}
或{{forloop.counter}}
{% for image in image_list %}
<div class="tab-pane" id="pic-{{ forloop.counter }}"><img src="http://placekitten.com/400/252" /></div>
{% endfor %}
{%用于图像列表%中的图像}
{%endfor%}
注意
{{forloop.counter}}
开始索引1
{{forloop.counter}}
开始索引0您可以使用{{forloop.counter}}变量。这将返回循环的索引
forloop.counter The current iteration of the loop (1-indexed)
forloop.counter0 The current iteration of the loop (0-indexed)
forloop.revcounter The number of iterations from the end of the loop (1-indexed)
forloop.revcounter0 The number of iterations from the end of the loop (0-indexed)
forloop.first True if this is the first time through the loop
forloop.last True if this is the last time through the loop
forloop.parentloop For nested loops, this is the loop surrounding the current one
有关内置模板、标签和过滤器的更多信息:循环中的pic是从2开始还是从1开始?
forloop.counter The current iteration of the loop (1-indexed)
forloop.counter0 The current iteration of the loop (0-indexed)
forloop.revcounter The number of iterations from the end of the loop (1-indexed)
forloop.revcounter0 The number of iterations from the end of the loop (0-indexed)
forloop.first True if this is the first time through the loop
forloop.last True if this is the last time through the loop
forloop.parentloop For nested loops, this is the loop surrounding the current one