gpt4 book ai didi

python - django 模板中的自定义计数器

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:53 25 4
gpt4 key购买 nike

我在 django 模板页面中有这段代码

<select class="selectpicker datatable-column-control" multiple
{% for q_group in question_groups %}
<optgroup label="{{ q_group.name }}">
{% for q in q_group.questions %}
<option value="{{ forloop.counter0 }}">{{ q.title }}</option>
{% endfor %}
</optgroup>
{% endfor %}

我想要在每次迭代中增加的每个选项标签的值。如果我有 10 个选项标签,那么它们的值将从 0 到 9。forloop.counter0 不能满足我的需要,因为当外循环完成一次时,内循环计数器初始化为 0。

最佳答案

传递一个itertools.count怎么样?反对模板?

模板:

<select class="selectpicker datatable-column-control" multiple>
{% for q_group in question_groups %}
<optgroup label="{{ q_group.name }}">
{% for q in q_group.questions %}
<option value="{{ counter }}">{{ q.title }}</option>
{% endfor %}
</optgroup>
{% endfor %}
</select>

查看:

import itertools
import functools

render(request, 'template.html', {
question_groups: ...,
counter: functools.partial(next, itertools.count()),
})

关于python - django 模板中的自定义计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34059246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com