I'm trying to retrieve data from the database and render it in rows of three columns. I've tried as many methods as I could find, finally it seemed to be rendering with this code:
<div class='container'>
<div class="row">
{% for category in categories %}
{% if not forloop.counter0|divisibleby:"3" %}
<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="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>
{% endif %}
{% endfor %}
</div>
It renders the elements in three columns but the columns are not aligned, and when checking the HTML, the 'row' class is the same for all the rows (giving it an id and checking by CSS), so I guess there's something I'm doing wrong.
I'd like to get an output like:
Category1 - Category2 - Category3
Category4 - Category5 - Category6
With the 'page' objects of each category underneath.
The data is rendering OK, the view is simple (just getting all the Category objects). I just need this kind of data rendering in different rows of 3 columns. I've tried the divisibleby method, but I guess I'm still missing something out. What would be the best approach?