Har*_*rry 3 python django django-templates django-template-filters
我有一个循环的图像.
期望的模式:
1 4 5 8 9 12 13 16
2 3 6 7 10 11 14 15
Run Code Online (Sandbox Code Playgroud)
根据forloop编号,我将有2个可能的图像尺寸,顶部的图像尺寸为1,底部的图像尺寸为2.
所以我使用divisibleby:"x"开始,但很快意识到这不会起作用,因为模式并不总是允许divisibleby,在某些情况下两者都可能是真的.
我可以轻易地做到这一点,以检查数字是否在列表中,如:
[2,3,6,7,14,15....]
Run Code Online (Sandbox Code Playgroud)
但这真的很愚蠢.
有一个简单的方法吗?
我最初的想法,根本无效!
{% for project in branding %}
{% if forloop.counter == 1 or forloop.counter|divisibleby:"4" or forloop.counter|divisibleby:"5" %}
<div class="tile">
<a href="/work/{{ project.slug }}/">
<img src="{% thumbnail project.tile_image "313x490" crop="center" as im %}{{ im.url }}{% endthumbnail %}">
</a>
</div>
{% endif %}
{% if forloop.counter|divisibleby:"2" or forloop.counter|divisibleby:"3" %}
<div class="tile">
<a href="/work/{{ project.slug }}/">
<img src="{% thumbnail project.tile_image "313x310" crop="center" as im %}{{ im.url }}{% endthumbnail %}">
</a>
</div>
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
小智 5
我相信你正在寻找你的数字mod 4是2或3的情况.
2 % 4 = 2
3 % 4 = 3
6 % 4 = 2
7 % 4 = 3
10 % 4 = 2
11 % 4 = 3
Run Code Online (Sandbox Code Playgroud)