2👍
There was a syntax error that made it render incorrectly, the following code renders correctly:
<section class="bg-light appear-animation h-100" data-appear-animation="fadeIn">
<div class='container'>
<div class="row">
{% for category in categories %}
{% if forloop.counter0|divisibleby:"3" %}
</div>
<div class="row">
<div class="col-6 col-md-4">
<h3>{{category.category_name}}</h3>
{% for page in category.page_set.all %}
<p>{{page.page_title}}</p>
{% endfor %}
</div>
{% else %}
<div class="col-6 col-md-4">
<h3>{{category.category_name}}</h3>
{% for page in category.page_set.all %}
<p>{{page.page_title}}</p>
{% endfor %}
</div>
{% endif %}
{% endfor %}
</div>
</section>
2👍
You have to use counter0 in the if around open row div tag.
Then you have to use counter in an if around close row div tag.
Because counter0 starts with 0 and counter starts with 1.
So a new row div tag is opened on 0, 3, 9, 12. And a created row div tag is closed on 2, 5, 8, 11.
<div class='container'>
{% for category in categories %}
{% if not forloop.counter0|divisibleby:"3" %}
<div class="row">
{% endif %}
<div class="col-6 col-md-4">
<h3>{{category.category_name}}</h3>
{% for page in category.page_set.all %}
<p>{{page.page_title}}</p>
{% endfor %}
</div>
{% if not forloop.counter|divisibleby:"3" %}
</div>
{% endif %}
{% endfor %}
- [Django]-Django REST: What's the recommended approach for supporting nested collections in URLs, such as /users/5/snippets/
- [Django]-Deploying Django application on Webfaction
Source:stackexchange.com