开发者

How to divide all objects into parts

开发者 https://www.devze.com 2023-04-11 21:11 出处:网络
I have for section in my template: {% for image in gallery.images.all %} <div id=\"image{{ image.id }}\" class=\"frame\">

I have for section in my template:

{% for image in gallery.images.all %}
    <div id="image{{ image.id }}" class="frame">
        <img src="{{ image.url_125x125 }}" alt="{{ image.name }}" />
    </div>
{% endfor %}

I want to divide this forloop section into parts, each with 5 elements for example, like this:

{% for image in gallery.im开发者_开发问答ages.all|slice:"5:" %}
    <div id="image{{ image.id }}" class="frame">
        <img src="{{ image.url_125x125 }}" alt="{{ image.name }}" />
    </div>
{% endfor %}
{% for image in gallery.images.all|slice:"5:5" %}
    <div id="image{{ image.id }}" class="frame">
        <img src="{{ image.url_125x125 }}" alt="{{ image.name }}" />
    </div>
{% endfor %}
{% for image in gallery.images.all|slice:"10:5" %}
    <div id="image{{ image.id }}" class="frame">
        <img src="{{ image.url_125x125 }}" alt="{{ image.name }}" />
    </div>
{% endfor %}

etc...

But I don't now how many images are in gallery. How to do it automatic? I know it's something of forloop.counter but I do not know how :) forloop.counter modulo 5 ?


could you just split it apart in your view? populate a list with lists of 5 elements until you run out? Then in your template you just iterate through each one of those 5 section chunks???

0

精彩评论

暂无评论...
验证码 换一张
取 消