0

I use Paginator for paginate my posts, i have no error, and i have the list of page in the bottom of the posts, but:

1.Post doesn't paginate correctly, i have set 5 i have more. 2. When i click on the 2 second page and 3 and etc, i have the same results of posts, i have no the next page with nexts posts.

This is my view code:

def post_all(request):
    posts = Post.objects.filter().order_by('-published_date')
    paginator = Paginator(posts, 5)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    categories = PostCategory.objects.all().annotate(posts_count=Count('post'))
    return render(request, 'front/blog/post_all.html', {"posts":posts, "categories":categories,"page_obj":page_obj})

My template:

<!-- Pagination -->
                                        <div class="pagination">
                                            {% if page_obj.has_previous %}
                                            <a href=""><i class="fa fa-angle-left"></i></a>
                                            {% endif %}
                                            <a href="" class="active">{{ page_obj.number }}</a> de <a href="" class="active">{{ page_obj.paginator.num_pages }}</a>
                                            {% if page_obj.has_next %}
                                            <a href="?page={{ page_obj.next_page_number }}"><i class="fa fa-angle-right"></i></a>
                                            {% endif %}
                                        </div>
                                        <!-- End Pagination -->

Thank u.

Créatux
  • 9
  • 5
  • can you show how you access the paginated queryset in your template? – Ersain Feb 22 '22 at 10:25
  • @Ersain yep, i have edited – Créatux Feb 22 '22 at 11:21
  • Okay, but it's only your pagination panel, can you show how you access the object_list, I assume you are not querying `page_obj.object_list`, but the posts. Therefore, you are getting the same unpaginated full query result. – Ersain Feb 22 '22 at 11:31
  • @Ersain sorry, i just make a for: {% for post in posts %} – Créatux Feb 22 '22 at 11:36
  • Yes, that's the problem, you don't need to loop through `posts`, neither you don't need to pass it as a context to the template. In order for it to work as expected, you need to loop through `page_obj.object_list` instead of `posts` in the for loop – Ersain Feb 22 '22 at 12:05
  • Hello Creatux, Please refer to this link will get all questions answers over there. https://stackoverflow.com/questions/70562651/django-pagination-not-showing-using-class-base-view/70562752#70562752 – Akash Nagtilak Feb 22 '22 at 13:15

1 Answers1

1

You could try using Class-based views.

Yours would be something like this :

views.py

from django.views.generic import (ListView)

class PostAllView(ListView):
    model = Post
    template_name = 'front/blog/post_all.html'  # <app>/<model>_<viewtype>.html
    context_object_name = 'posts'
    ordering = ['-published_date']
    paginate_by = 5

and a template.html that you can adapt to yours:

<h1>Total of posts : {{ page_obj.paginator.count }}</h1>
{% for post in posts %}
    <article>
        <p class="article-content">{{ post.content }}</p>
        <!-- all the post related content you want to display here-->
    </article>
{% endfor %}

{% if is_paginated %}

  {% if page_obj.has_previous %}
    <!-- you can adapt the class to your use-->
    <a class="button" href="?page=1">First</a>
    <a class="button" href="?page={{ page_obj.previous_page_number }}">Previous</a>
  {% endif %}

  {% for num in page_obj.paginator.page_range %}
    {% if page_obj.number == num %}
      <a class="button-strong" href="?page={{ num }}">{{ num }}</a>
    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
      <a class="button" href="?page={{ num }}">{{ num }}</a>
    {% endif %}
  {% endfor %}

  {% if page_obj.has_next %}
    <a class="button" href="?page={{ page_obj.next_page_number }}">Next</a>
    <a class="button" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
  {% endif %}

{% endif %}
CharlesV
  • 60
  • 8