在django模板中将卡片分成行的最佳方法

时间:2018-03-17 00:27:17

标签: django

What I have(image) 所以我需要将这些卡分成每行4-5张卡。 我的解决方案很简单

<div class="card-deck">
    {% for book in books %}
        {% include 'books/book_element.html' %}
    {% endfor %}
</div>

我已经尝试了一段时间,正在做一些头脑风暴

    {% for book in books %}
        {% if  forloop.counter|divisibleby:"5" or forloop.counter == 1 %}
            <div class="card-deck">
            </div>
        {% endif %}
    {% endfor %}

如何将模板包含在div.card-deck中?

1 个答案:

答案 0 :(得分:1)

您需要关闭上一行并在循环中打开一个新行。像这样:

<div class="card-deck">
    {% for book in books %}
        {% include 'books/book_element.html' %}
        {% if forloop.counter|divisibleby:"5"  %}
            {# Close the current deck and start a new one %}
            </div><div class="card-deck">
        {% endif %}
    {% endfor %}
</div><!-- The final deck is closed here, outside the loop -->