Search code examples
pythondjangodjango-templatesdjango-viewsdjango-pagination

How to perform pagination for context object in django?


I have tried something like this in views.py:

class HomePage(TemplateView):
     template_name = "clouderp/index.html"

    def get_context_data(self, **kwargs):
        context = super(HomePage, self).get_context_data(**kwargs) 
        qs = Blog.objects.all()
        context['blog_list'] = qs

        page = self.request.GET.get('page')

        paginator = Paginator(qs, 4)

        try:
            users = paginator.page(page)
        except PageNotAnInteger:
            users = paginator.page(1)
        except EmptyPage:
            users = paginator.page(paginator.num_pages)

        context['users'] = users

        return context

And in template:

{% if users.has_other_pages %}
<ul class="pagination">
  {% if users.has_previous %}
    <li><a href="?page={{ users.previous_page_number }}">&laquo;</a></li>
  {% else %}
    <li class="disabled"><span>&laquo;</span></li>
  {% endif %}
  {% for i in users.paginator.page_range %}
    {% if users.number == i %}
      <li class="active"><span>{{ i }} <span class="sr-only"></span></span></li>
    {% else %}
      <li><a href="?page={{ i }}">{{ i }}</a></li>
    {% endif %}
  {% endfor %}
  {% if users.has_next %}
    <li><a href="?page={{ users.next_page_number }}">&raquo;</a></li>
  {% else %}
    <li class="disabled"><span>&raquo;</span></li>
  {% endif %}
</ul>
{% endif %}

I have created a blog application using django
I want to show all my blogs in my main index.html page and also wanted to do pagination for the blogs in my index page...
I was wondering how to do it...
Because the process I have done the blogs are not paginated according to 4 at a time...


Solution

  • Please try this code as below: in views.py code

        class BookListView(ListView):
        model=Book
        paginate_by=10
        template_name='book/book_list.html'
    
        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            print (context)
            paginator = context['paginator']
            page_numbers_range = 10  # Display 5 page numbers
            max_index = len(paginator.page_range)
    
            page = self.request.GET.get('page')
            print (self.request)
            current_page = int(page) if page else 1
    
            start_index = int((current_page - 1) / page_numbers_range) * page_numbers_range
            end_index = start_index + page_numbers_range
            if end_index >= max_index:
                end_index = max_index
    
            page_range = paginator.page_range[start_index:end_index]
            context['page_range'] = page_range
            return context
    
    book_list = BookListView.as_view()
    

    and the template:

    <div class="container">
                <!-- Pagination -->
                {% if is_paginated %}
                <nav>
                    <ul class="pagination justify-content-center" style="margin:20px 0">
                    {% if page_obj.has_previous %}
                        <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}">
                            <span>Prev</span>
                        </a>
                        </li>
                    {% else %}
                        <li class="disabled page-item">
                        <a class="page-link" href="#">
                            <span>Prev</span>
                        </a>
                        </li>
                    {% endif %}
    
                    {% for page in page_range %}
                        <li {% if page == page_obj.number %} class="active page-item" {% endif %}>
                        <a class="page-link" href="?page={{ page }}">{{ page }}</a>
                        </li>
                    {% endfor %}
                    {% if page_obj.has_next %}
                        <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.next_page_number }}">
                            <span>Next</span>
                        </a>
                        </li>
                    {% else %}
                        <li {% if not page_obj.has_next %}class="disabled page-item"{% endif %}>
                        <a class="page-link" href="#">
                            <span>Next</span>
                        </a>
                        </li>
                    {% endif %}
                    </ul>
                </nav>
                {% endif %}
        </div>