在我的django模板中,我有以下for循环来显示days。我想知道,是否有可能在循环中迭代一个数字(在下面的例子中是i)。或者我必须将其存储在数据库中,然后以days.day_number的形式进行查询?
{% for days in days_list %}
<h2># Day {{ i }} - From {{ days.from_location }} to {{ days.to_location }}</h2>
{% endfor %}
发布于 2017-10-25 16:50:46
还可以使用下面的代码:
{% if forloop.first %}
或
{% if forloop.last %}
发布于 2020-12-18 19:00:09
{% for days in days_list %}
<h2># Day {{ forloop.counter }} - From {{ days.from_location }} to {{ days.to_location }}</h2>
{% endfor %}
或者如果您想从0开始
{% for days in days_list %}
<h2># Day {{ forloop.counter0 }} - From {{ days.from_location }} to {{ days.to_location }}</h2>
{% endfor %}
发布于 2021-08-24 09:59:34
像这样做,
{% for days in days_list %}
<h2># Day {{ forloop.counter }} - From {{ days.from_location }} to {{ days.to_location }}</h2>
{% endfor %}
https://stackoverflow.com/questions/11481499
复制相似问题