[Answered ]-Django pagination return all elements but still paginate

2👍

I assume you are using the MultipleObjectMixin.

In this case the pagination happens when you call context.update(self.get_context_data(form=form)) . See the source there : https://github.com/django/django/blob/master/django/views/generic/list.py

So when you call this function, it sets context[‘object_list’] to the paginated content. Unfortunately you override it a few lines after that when you call context['object_list'] = self.object_list, because self.object_list is not impacted by the pagination. If you delete this line it should therefore be fine.

edit : As it seems you were using ‘article_list’ and not ‘object_list’, here are additional comment:

  • in your original function, at the end you had ‘page’ that refers to the righ pagination function, and ‘article_list’ and ‘object_list’ that refered to the full list of articles as I explained.
  • with my solution, you still had ‘page’ and ‘article_list’ unchanged, but ‘object_list’ was referring to the right paginated list. Unfortunately I did not notice it was not the one you were using in the template.
  • So now all you have to do is replace context[self.context_object_name] = self.object_list by context[self.context_object_name] = context['object_list'], and it will work 🙂
👤Mijamo

0👍

So, at the moment I have a temporary solution which is :

{% for article in page_obj.object_list %}
        <div class="row article">
            <div class="col-md-2">
                {{ article.date_realization|date:"F d, Y" }}
            </div>
            <div class="col-md-2">
                {{ article.source }}
            </div>
            <div class="col-md-2">
            {% for region in article.regions.all %}
                {{ region.name }}
            {% endfor %}
            </div>
            <div class="col-md-6">
                {{ article.title }}
            </div>
        </div>
        {% endfor %}

instead of doing :

{% for article in article_list %}
    <div class="row article">
        <div class="col-md-2">
            {{ article.date_realization|date:"F d, Y" }}
        </div>
        <div class="col-md-2">
            {{ article.source }}
        </div>
        <div class="col-md-2">
        {% for region in article.regions.all %}
            {{ region.name }}
        {% endfor %}
        </div>
        <div class="col-md-6">
            {{ article.title }}
        </div>
    </div>
    {% endfor %}

article_list became object_list. I am not very happy with it since when I read the doc this should not be necessary.

Leave a comment