django template rows of multiple items

前端 未结 4 728
梦毁少年i
梦毁少年i 2020-12-13 15:26

I\'m creating a catalogue, where there is a list of items of undefined length. I want to spit it out in rows with three columns each. So I have the following html:



        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 16:07

    You can use forloop.counter variable and divisibleby filter. The code will be close to this:

    {% for item in items %}
        {% if forloop.first %}
            
    {% endif %}
    {{ item }}
    {% if forloop.counter|divisibleby:"3" %}
    {% if not forloop.last %}
    {% endif %} {% endif %} {% if forloop.last %}
    {% endif %} {% endfor %}

提交回复
热议问题