Home > Mobile >  Django Paginator shows post titles Not Page numbers, why?
Django Paginator shows post titles Not Page numbers, why?

Time:09-22

i'm trying to use Django Paginator using ListView, but the paginator shows post title instead of page number: Page number replaced with post title

how can i fix that? this is my code: in views.py:

from django.shortcuts import render
from django.core.paginator import Paginator
from django.views.generic import ListView, DetailView
from .models import Post

# Create your views here.


# PostObject_list by using ListView
class PostList(ListView):
    model=Post
    paginate_by=20
    context_object_name='all_posts'
    ordering=['-created_at']


# allpost by using ListView
'''class AllPost(ListView):
    model=Post
    context_object_name='allpost'
    paginate_by=None
'''    

# Post_singl_object using DetailView
class PostDetail(DetailView):
    model=Post

in template post_list.html :

 </div>
            <nav aria-label="Page navigation example">
                <ul class="pagination d-flex justify-content-center mt-1">
                    {% if all_posts.has_previous  %}
                    <li class="page-item "><a class="page-link" href="?page={{ all_posts.previous_page_number }}">Previous</a></li>
                    {% endif %}

                    {% for page in all_posts %}
                        <li class="page-item ">
                        
                             <a class="page-link" href="?page={{ page }}"><span>{{page}}</span></a>
                        </li>
                    {% endfor %}
                 
                    {% if all_posts.has_next %}
                         <li class="page-item"><a class="page-link" href="?page={{all_posts.previous_page_number }}">Next</a></li>
                    {% endif %}
                </ul>
            </nav>
            </div>
            <!-- Pagination-->

thank you!

CodePudding user response:

Using all_posts will not work because this is the object list, not the page object. all_posts will not have the page attributes like paginator, has_previous, previous_page_number, etc.

In this case you need to use page_obj, which is automatically added by ListView to the template context when paginating. You can get the page range from the template using page_obj.paginator.page_range:

{% for page in page_obj.paginator.page_range %}
<li class="page-item ">
   <a class="page-link" href="?page={{ page }}"><span>{{ page }}</span></a>
</li>
{% endfor %}

The implementation for Previous and Next will also not work because of the same reason. To resolve that, you also need to use page_obj, so all in all:

{% if page_obj.has_previous  %}
    <li class="page-item "><a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a></li>
{% endif %}

{% for page in page_obj.paginator.page_range %}
    <li class="page-item ">
       <a class="page-link" href="?page={{ page }}"><span>{{ page }}</span></a>
    </li>
{% endfor %}

{% if page_obj.has_next %}
     <li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a></li>
{% endif %}

CodePudding user response:

You should access to paginator. In your case, page_obj.paginator. Try to change your template to:

{% for i in page_obj.paginator.page_range %}
<li class="page-item ">
   <a class="page-link" href="?page={{ page }}"><span>{{page}}</span></a>
</li>
{% endfor %}
  • Related