1👍
There are two separate problems at work here which have been indicated in the above answers:
-
You are not using Bootstrap correctly: although you can append multiple
<div class="col-sm-4">
together, you will see the irregular collapsing behavior in your screenshot if they are different heights. The purpose of<div class="row">
is to ensure that your columns will appear in separate rows. See Must Bootstrap container elements include row elements? for more information.You can resolve this with code like the following in your for-loop to add a new row every third item:
{% if forloop.counter|divisibleby:3 %} </div> <div class="row"> {% endif %}
-
You are not using the correct context object in your template: The paginator object is passed as
news_set
in your context object but the template uses another context object:newsInCat
, which is not paginated. If you follow @Sayse’s suggestion of using thenews_set
object, you should be in good shape:{% for news in news_set %}
As a final suggestion, the <article>
tag does not seem to be doing anything besides giving semantic value. Why not just use it instead of the div, so that you have <article class="col-sm-4">
?
And as a final note, camelCase is generally frowned on in Python. Try using_underscores_with_lowercase, like you’ve already done with news_set
.
Adding all these suggestions, you would only need to amend your template to something like this:
<div class="row">
{% for news in news_set %}
<article class="col-sm-4">
<!-- add your article content here...and clean it up! You have unnecessary spaces, inconsistent use of single and double quotes, and inline styles that (probably) should be defined in an external stylesheet. -->
</article>
{% if forloop.counter|divisibleby:3 %}
</div>
<div class="row">
{% endif %}
{% endfor %}
</div>
0👍
I can speak to your HTML/CSS formatting problem. col-sm-4
tells me you’re using Bootstrap, which defines layouts in terms of 12 column width. Bootstrap will always attempt to make the with of all rows equal 12.
Right now, you’re looping over all the objects and adding n columns each of width col-sm-4
. Booststrap is trying to make sure each takes up a third of the row, but you’re adding more that 3 divs, which is more than 12 total column width. Once the column is full, (starting with the 4th news item), Bootstrap moves the divs as closely as it can to the top of the row
while still obeying the rule that width can only be 12.
In the case you pictured, Bootstrap offsets the 4th and 5th of divs because the offset allows them to be closer to the top of the row
.
To fix this, you’d need to have each set of three news items in it’s own row, so that your 3 col-sm-4
divs total a width of 12.
- Django HTTP 404 error when PUT or GET
- How to do an OR on a Django ManyToManyField without getting duplicates?
- Django allauth: empty 'signup_url'
- Huge latency with python model
0👍
Judging based off the template, you must be still including the full list as well as the paginated set
In your template you are iterating over newsInCat
instead of news_set
{% for news in newsInCat %}
should be
{% for news in news_set %}
- Gunicorn does not find main module with docker
- Apache Django webpage is not saving values for long period of time
- Compare data from two different models with Django
- Adding Image into Django Admin ImageField and displaying it in html
- Django Template getting apparent catchall error: "AssertionError at / No exception message supplied"