Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display only some of the page numbers by django pagination

I am using the django paginator in the template. Its working ok, but not good when there's large numbers of pages.

views.py:

def blog(request):     blogs_list = Blog.objects.all()      paginator = Paginator(blogs_list, 1)      try:         page = int(request.GET.get('page', '1'))     except:         page = 1      try:         blogs = paginator.page(page)     except(EmptyPage, InvalidPage):         blogs = paginator.page(page)     return render(request, 'blogs.html', {         'blogs':blogs         }) 

snippet of the template:

  <div class="prev_next">      {% if blogs.has_previous %}       <a class="prev btn btn-info" href="?page={{blogs.previous_page_number}}">Prev</a>     {% endif %}     {% if blogs.has_next %}       <a class="next btn btn-info" href="?page={{blogs.next_page_number}}">Next</a>     {% endif %}     <div class="pages">       <ul>       {% for pg in blogs.paginator.page_range %}         {% if blogs.number == pg %}           <li><a href="?page={{pg}}" class="btn btn-default">{{pg}}</a></li>         {% else %}           <li><a href="?page={{pg}}" class="btn">{{pg}}</a></li>         {% endif %}       {% endfor %}       </ul>     </div>     <span class="clear_both"></span>    </div>  

Now it looks like this:

enter image description here

What do I do to display only 7 page numbers and not all of it ranging from the current page number, like this:

Prev 1 (2) 3 4 5 Next 

I hope I was clear, if not please ask. Your help and guidance will be very much appreciated. Thank you.

like image 558
Robin Avatar asked Jun 16 '15 09:06

Robin


People also ask

How does django deal with Pagination?

Note that you can give Paginator a list/tuple, a Django QuerySet , or any other object with a count() or __len__() method. When determining the number of objects contained in the passed object, Paginator will first try calling count() , then fallback to using len() if the passed object has no count() method.

What is paginator in django?

Django provides a few classes that help you manage paginated data – that is, data that's split across several pages, with “Previous/Next” links. These classes live in django/core/paginator.py. For examples, see the Pagination topic guide.

Does Pagination improve performance django?

Additionally, switching to keyset pagination will improve the performance of page lookups and make them work in constant time. Django makes it easy to alter its default configuration, giving you the power to build a performant solution for pagination in Django.


1 Answers

Gonna throw this in. I came up with it because it lets you know there are more pages on either side.

<ul class="pagination">  {% if page_obj.has_previous %}     <li><a href="?page={{ page_obj.previous_page_number }}"><i class="fa fa-chevron-left" aria-hidden="true"></i></a></li> {% else %}     <li class="disabled"><span><i class="fa fa-chevron-left" aria-hidden="true"></i></span></li> {% endif %}  {% if page_obj.number|add:'-4' > 1 %}     <li><a href="?page={{ page_obj.number|add:'-5' }}">&hellip;</a></li> {% endif %}  {% for i in page_obj.paginator.page_range %}     {% if page_obj.number == i %}         <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>     {% elif i > page_obj.number|add:'-5' and i < page_obj.number|add:'5' %}         <li><a href="?page={{ i }}">{{ i }}</a></li>     {% endif %} {% endfor %}  {% if page_obj.paginator.num_pages > page_obj.number|add:'4' %}     <li><a href="?page={{ page_obj.number|add:'5' }}">&hellip;</a></li> {% endif %}  {% if page_obj.has_next %}     <li><a href="?page={{ page_obj.next_page_number }}"><i class="fa fa-chevron-right" aria-hidden="true"></i></a></li> {% else %}     <li class="disabled"><span><i class="fa fa-chevron-right" aria-hidden="true"></i></span></li> {% endif %}  </ul> 

And it looks like this:

Paging with elipses

like image 131
Rob L Avatar answered Oct 16 '22 08:10

Rob L