我正在使用带有Django的Bootstrap,并希望.item-container.col-md-4在一行中是三个盒子。看起来应该是这样的:
<div class="row">
<div class="item-container col-md-4> Stuff</div>
<div class="item-container col-md-4> Another Thing</div>
<div class="item-container col-md-4> This Next One</div>
</div>
我得到更像这样的东西:
<div class="row">
<div class="item-container col-md-4> Stuff
<div class="item-container col-md-4> Another Thing</div>
</div>
</div>
<div class="item-container col-md-4> This Next One</div>
这是我的代码:
{% for product in products %}
{% if forloop.first %}
<div class="row">
{% endif %}
<div class="item-container col-md-4">
{{ product.someinfo }}
</div>
{% if forloop.counter != products|length %}
</div>
<script> console.log('not last', {{ products|length }}, {{forloop.counter}} ); </script>
{% endif %}
{% if forloop.last %}
</div>
<script> console.log('by 3', {{ products|length }}, {{forloop.counter}} );</script>
{% elif forloop.counter|divisibleby:3 %}
<script> console.log('last', {{ products|length }}, {{forloop.counter}} );</script>
<div class='row'>
{% endif %}
{% empty %}
<div class="nothing-found">
Nothing found.
</div>
{% endfor %}
答案 0 :(得分:3)
您可能需要删除下面显示的行中的额外<div>
{% if forloop.counter != products|length %}
</div> <----------------------- THIS
<script> console.log('not last', {{ products|length }}, {{forloop.counter}} ); </script>
{% endif %}
我建议将模板重写为更简单的
{% if products %}
<div class="row">
{% for product in products %}
<div class="item-container col-md-4"> {{ product.someinfo }} </div>
{% if forloop.counter|divisibleby:3 %}
</div>
<div class="row">
{% endif %}
{% endfor %}
</div>
{% else %}
<div class="nothing-found">
Nothing found.
</div>
{% endif %}