Django模板算术 [英] Django Template Arithmetic
问题描述
在我的模板中,我遍历一个列表,尝试进行两列布局。由于需要两列布局,因此我需要在for循环中编写的标记取决于 forloop.counter0
是偶数还是奇数。如果我具有使用模板语言编写的Python的全部功能,那么确定 forloop.counter0
的奇偶校验将是微不足道的,但不幸的是,并非如此。我如何使用Django模板语言测试 forloop.counter0
是偶数还是奇数,还是一样好,还有另一种方法可以使列表中的元素交替显示吗?
In my template, I am looping through a list, trying to make a two-column layout. Because of the desired two-column layout, the markup I need to write in the for loop is dependent on whether forloop.counter0
is even or odd. If I had the full power of Python in the template language, determining the parity of forloop.counter0
would be trivial, but unfortunately that is not the case. How can I test whether forloop.counter0
is even or odd using the Django template language, or just as good, is there another way I could get elements in the list to display alternatively in the left and right columns?
预先感谢!
推荐答案
您可以将 divisibleby 过滤器与<$ c $一起使用c> forloop.counter :
{% if forloop.counter|divisibleby:"2" %}even{% else %}odd{% endif %}
这篇关于Django模板算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!