Skip to main content
# Keyset Pagination for Django.

Django pagination uses the LIMIT/OFFSET method. This is fine for smaller offsets, but once you start getting beyond a few pages, it can perform really badly. This is because the database needs to fetch all of the previous rows, even though it discards them.

Using Keyset Pagination allows for better performing "next page" fetches, at the cost of not being able to randomly fetch a page. That is, if you know the last element from page N-1, then you may fetch page N, but otherwise you really can't.

Keyset Pagination, sometimes called the Seek Method, has been documented by [Markus Winand](https://use-the-index-luke.com/sql/partial-results/fetch-next-page) and [Joe Nelson](https://www.citusdata.com/blog/2016/03/30/five-ways-to-paginate/). If you are not familiar with the concept, I strongly suggest you read the articles above.

In order to use the paginator within this package, you will probably also need to use the provided View mixin: this changes the way a queryset is paginated to enable non-integer "page numbers".

class List(PaginationMixin, ListView):
paginator_class = KeysetPaginator
paginate_by = 10
queryset = MyModel.objects.order_by('-timestamp', 'group')

You won't be able to iterate through page numbers in a template in the same way: you are limited to next and previous pages. Otherwise, you construct them in largely the same way:

<a href="{% url 'mymodel:list' %}?page={{ page_obj.previous_page_number }}">
Prev Page
</a>

<a href="{% url 'mymodel:list' %}?page={{ page_obj.next_page_number }}">
Next Page
</a>

Note that you do not get access to the length of the queryset, nor the number of pages, because these could be expensive queries. You really don't need to know that ;)

However, I like to use GET forms to [enable pagination of filtered results](https://schinckel.net/2014/08/17/leveraging-html-and-django-forms%3A-pagination-of-filtered-results/):

<button form="target-form"
name="page"
value="{{ page_obj.previous_page_number }}"
type="submit">
&larr; Prev Page
</button>

<button form="target-form"
name="page"
value="{{ page_obj.next_page_number }}"
type="submit">
Next Page &rarr;
<button>


See https://schinckel.net/2018/11/23/keyset-pagination-in-django/ for more details about how this package works.


Metadata

Release files for django-keyset-pagination-plus 0.9.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-keyset-pagination-plus 0.9.3
File Size Uploaded Provenance Actions
django-keyset-pagination-plus-0.9.3.tar.gz 6.9 kB Without provenance Details for django-keyset-pagination-plus-0.9.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-keyset-pagination-plus 0.9.3
File Interpreter ABI Platform Actions
django_keyset_pagination_plus-0.9.3-py2.py3-none-any.whl Python 2, Python 3 none any Details for django_keyset_pagination_plus-0.9.3-py2.py3-none-any.whl

Total release size: 14.5 kB

Release files / django-keyset-pagination-plus-0.9.3.tar.gz

Download URL django-keyset-pagination-plus-0.9.3.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
0a46ae5f8280199f0806943fb5d1fa7b75ccd893018a12df25906271c340e8fe
BLAKE2b-256 checksum
How to use checksums
a7e4ac2de142310d00618fae60c54fd35d82cf6bcee3884ad9b4caabb273400c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/3.7.1

Release files / django_keyset_pagination_plus-0.9.3-py2.py3-none-any.whl

Download URL django_keyset_pagination_plus-0.9.3-py2.py3-none-any.whl
Size 7.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7e39c9d847afd6edc90cfd2a22a4e938e05c3957870d17f37000da5d6cd04b49
BLAKE2b-256 checksum
How to use checksums
cf4c8b8f9c9c5f3d4019f71734642aaf8f7041ad2842e4f5600371e36410ff8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/3.7.1

Security for django-keyset-pagination-plus 0.9.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page