Django,模板,循环和循环 [英] Django, templates, for loops, and cycles
问题描述
(底部的tl)
让我尝试解释一下我要完成的工作:我有一个二维数组,我想以某种方式显示其内容。我想要行,并且每行最多只能显示三个对象,因为缺少更好的词。因此,我想遍历数组并在此过程中创建HTML。我的想法是:数组中的每个三个中的第一个元素都应打开行。每三分之三元素应关闭行。但是,如果[inner]数组中的最后一个元素不是恰好是三分之三,则仍应关闭该行。因此,例如,如果我们有类似 L = [[0,1,2,3,4],[5,6,7]]
的东西,可以这样显示:
Let me try to explain what I'm trying to accomplish: I have a two dimensional array, and I would like to display its contents a certain way. I want "rows", and each row can display no more than three "objects", for lack of a better word. So I want to iterate over the array and create my HTML in the process. My idea is this: every "first of three" elements in the array should open up the "row". Every "third of three" elements should close the "row". However, if the last element in the [inner] array does not happen to be the "third of three", it should still close the row. So, for example, if we had something like L=[ [0,1,2,3,4], [5,6,7] ]
, I would want to display it like so:
0 1 2
3 4
5 6 7
可能被标记为:
<div>0 1 2</div>
<div>3 4</div>
<div>5 6 7</div>
我的第一个想法是简单地使用模运算符,看看每次迭代是否都是第一个, 第二或第三行,但是Django模板不直接支持模(稍后会详细介绍)。
My first thought was to simply use the modulo operator and see if each iteration was the "first", "second", or "third" of a row, but Django templates don't directly support modulo (more on that later).
所以我想到了这样的模板代码:
So I came up with template code like this:
{% for x in L %}
{% for y in x %}
{% cycle '<div>' '' '' %}
{{ y }}
{% cycle '' '' '</div>' %}
{% endfor %}<br/>
{% endfor %}
这是一个好的开始。以上面的示例为例,在 0
上我们将打开一个div,在 1
上不执行任何操作,在 2
关闭div,在 3
上打开,然后在 4
上...好吧,它不会关闭,因为 4
不是 3系列中的第3个。因此,虽然Django模板没有取模,但它们确实有一个dibyby check,因此我想出了另外的逻辑,如果我们点击[inner] for循环的最后一个元素,并且也不能被3整除(因此我们没有重复的关闭),然后关闭div:
And this was a good start. Taking the above example, on 0
we would open a div, on 1
do nothing, on 2
close the div, on 3
open it, and on 4
... well, it wouldn't close, because 4
was not the "third in a series of 3". So, while Django templates don't have modulo, they do have a divisibleby check, so I came up with additional logic to say, if we hit the last element of the [inner] for loop, and it also is not divisible by 3 (so we don't have a duplicate close), then close the div:
{% for x in z %}
{% for y in x %}
{% cycle '<div>' '' '' %}
{{ y }}
{% cycle '' '' '</div>' %}
{% if forloop.last %}
{% if forloop.counter|divisibleby:"3" %}
<!-- Row Already Closed -->
{% else %}
</div>
{% endif %}
{% endif %}
{% endfor %}<br/>
{% endfor %}
效果更好!现在,我以正确的标记遍历了整个第一个内部数组。我的问题是:显然,当您超出内部for循环的范围时,Django的循环功能不会重置。这意味着,我在上面示例中的数字 5
并没有像应该打开的div一样,也不被认为是周期中的第一个。实际上,它实际上被认为是第三名,因此它正在关闭div!
This worked better! Now, I got through the whole first inner array with proper markup. My issue is this: apparently, Django's cycle functionality does not reset when you go out of scope of the inner for loop. What this means is, my number 5
from the above example is not opening a div like it should, it is not being recognized as the first in a cycle. In fact, it's actually being recognized as a third, and so it is closing a div!
所以我不确定从这里去哪里。这似乎是 Django中已知和未解决的问题。有人可以帮忙吗?
So I'm not sure where to go from here. This appears to be a known and unfixed issues in Django. Can anyone assist, please?
tl; dr 我想使用二维数组,例如 L = [[0,1,2,3,4],[5,6,7]]
并正确地将其标记为一次,每次分组最多3个而不将多个数组中的任何元素分组在一起,就像这样:
tl;dr I want to take a 2d array, e.g. L=[ [0,1,2,3,4], [5,6,7] ]
and properly mark it up grouping no more than 3 at a time and without grouping any elements from multiple arrays together, like so:
0 1 2
3 4
5 6 7
模板代码是什么,为什么?
What would be the template code for that and why?
推荐答案
您应该能够使用 {%,如果forloop.counter0 | dibyby: 3% }
确定何时打开< div>
标记,以及 {%如果是forloop.last或forloop.counter | dibyby: 3%}
确定何时关闭< / div>
标记。
You should be able to use {% if forloop.counter0|divisibleby:"3" %}
to determine when to open the <div>
tag, and {% if forloop.last or forloop.counter|divisibleby:"3" %}
to determine when to close the </div>
tag.
{% for x in z %}
{% for y in x %}
{% if forloop.counter0|divisibleby:"3" %}<div>{% endif %}
{{ y }}
{% if forloop.last or forloop.counter|divisibleby:"3" %}</div>{% endif %}
{% endfor %}<br/>
{% endfor %}
这篇关于Django,模板,循环和循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!