0
這是一個奇怪的問題,但我希望能夠在我的模板上使用不同的{% for loops %}
多次顯示相同的查詢集,我試圖避免複製每個{% for loops %}
。是否可以在模板上多次顯示相同的查詢集?
比方說,我有我的views.py這樣的:
...
chicken = Chicken.objects.filter(id=1)
return render(request, 'chicken.html', {'chicken':chicken}
chicken.html:下面的例子是一個我回避。
<!-- {{ egg }} has the same value everywhere -->
{% for egg in chicken %} <!-- has a different index than other loops -->
<p id="egg_1">{{egg}}</p>
{% endfor %}
{% for egg in chicken %}
<p id="egg_2">{{egg}}</p>
{% endfor %}
{% for egg in chicken %}
<p id="egg_3">{{egg}}</p>
{% endfor %}
...x52...
有沒有辦法來automatise其在每次循環不同的索引,而這個?
我在尋找這樣的事情:
{% for chicken x52 %}
{% for egg in chicken %}
<p id="egg_index">{{egg}}</p> <!-- each with different index -->
{% endfor %}
{% endfor %}
正是我在找的:) – Lindow