Pagination

Django provides high-level and low-level ways to help you manage paginated data – that is, data that’s split across several pages, with “Previous/Next” links.

The Paginator class

Under the hood, all methods of pagination use the Paginator class. It does all the heavy lifting of actually splitting a QuerySet into Page objects.

Example

Give Paginator a list of objects, plus the number of items you’d like to have on each page, and it gives you methods for accessing the items for each page:

  1. >>> from django.core.paginator import Paginator
  2. >>> objects = ["john", "paul", "george", "ringo"]
  3. >>> p = Paginator(objects, 2)
  4. >>> p.count
  5. 4
  6. >>> p.num_pages
  7. 2
  8. >>> type(p.page_range)
  9. <class 'range_iterator'>
  10. >>> p.page_range
  11. range(1, 3)
  12. >>> page1 = p.page(1)
  13. >>> page1
  14. <Page 1 of 2>
  15. >>> page1.object_list
  16. ['john', 'paul']
  17. >>> page2 = p.page(2)
  18. >>> page2.object_list
  19. ['george', 'ringo']
  20. >>> page2.has_next()
  21. False
  22. >>> page2.has_previous()
  23. True
  24. >>> page2.has_other_pages()
  25. True
  26. >>> page2.next_page_number()
  27. Traceback (most recent call last):
  28. ...
  29. EmptyPage: That page contains no results
  30. >>> page2.previous_page_number()
  31. 1
  32. >>> page2.start_index() # The 1-based index of the first item on this page
  33. 3
  34. >>> page2.end_index() # The 1-based index of the last item on this page
  35. 4
  36. >>> p.page(0)
  37. Traceback (most recent call last):
  38. ...
  39. EmptyPage: That page number is less than 1
  40. >>> p.page(3)
  41. Traceback (most recent call last):
  42. ...
  43. EmptyPage: That page contains no results

Note

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. This allows objects such as Django’s QuerySet to use a more efficient count() method when available.

Paginating a ListView

django.views.generic.list.ListView provides a builtin way to paginate the displayed list. You can do this by adding a paginate_by attribute to your view class, for example:

  1. from django.views.generic import ListView
  2. from myapp.models import Contact
  3. class ContactListView(ListView):
  4. paginate_by = 2
  5. model = Contact

This limits the number of objects per page and adds a paginator and page_obj to the context. To allow your users to navigate between pages, add links to the next and previous page, in your template like this:

  1. {% for contact in page_obj %}
  2. {# Each "contact" is a Contact model object. #}
  3. {{ contact.full_name|upper }}<br>
  4. ...
  5. {% endfor %}
  6. <div class="pagination">
  7. <span class="step-links">
  8. {% if page_obj.has_previous %}
  9. <a href="?page=1">&laquo; first</a>
  10. <a href="?page={{ page_obj.previous_page_number }}">previous</a>
  11. {% endif %}
  12. <span class="current">
  13. Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
  14. </span>
  15. {% if page_obj.has_next %}
  16. <a href="?page={{ page_obj.next_page_number }}">next</a>
  17. <a href="?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>
  18. {% endif %}
  19. </span>
  20. </div>

Using Paginator in a view function

Here’s an example using Paginator in a view function to paginate a queryset:

  1. from django.core.paginator import Paginator
  2. from django.shortcuts import render
  3. from myapp.models import Contact
  4. def listing(request):
  5. contact_list = Contact.objects.all()
  6. paginator = Paginator(contact_list, 25) # Show 25 contacts per page.
  7. page_number = request.GET.get("page")
  8. page_obj = paginator.get_page(page_number)
  9. return render(request, "list.html", {"page_obj": page_obj})

In the template list.html, you can include navigation between pages in the same way as in the template for the ListView above.