gpt4 book ai didi

python - Django for 循环的倒数第二个迭代

转载 作者:行者123 更新时间:2023-11-28 21:50:38 24 4
gpt4 key购买 nike

tabbed_fieldset.html 指出:

    if forloop.counter0 > 0 and forloop.last
#do stuff`

我只是写在一个单独的文件中:

for fieldset in adminform
if not forloop.last
include "admin/includes/tabbed_fieldset.html"
else
include "admin/includes/geo_fieldset.html"
endif
endfor

所以现在 tabbed_fieldset.html 的代码将不再有效,因为如果它是 forloop.last,它甚至不会在 tabbed_fieldset 中。 html,它将在 geo_fieldset.html 中。

有没有简单的方法把forloop.last改成forloop second to last

最佳答案

感谢您的代码!查看此网站:https://docs.djangoproject.com/en/1.8/ref/templates/builtins/

The for loop sets a number of variables available within the loop:

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

基本上,您会使用 forloop.revcounterforloop.revcounter0

for fieldset in adminform
if forloop.revcounter0 > 1
include "admin/includes/tabbed_fieldset.html"
else
include "admin/includes/geo_fieldset.html"
endif
endfor

关于python - Django for 循环的倒数第二个迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31734730/

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