Link Header Pagination for Django Rest Framework
Project description
djangorestframework-link-header-pagination
Overview
Provide pagination using a Link HTTP header as described in GitHub's REST API documentation.
This pagination style accepts a single page number in the request query parameters. The response uses an HTTP header called Link to provide the URLs for the next, previous, first, and last pages. If you are using Python's Requests library to make the request, this header is automatically parsed for you as described here.
Request:
GET https://api.example.org/accounts/?page=4
Response:
HTTP 200 OK
Link: <https://api.example.org/accounts/>; rel="first", <https://api.example.org/accounts/?page=3>; rel="prev", <https://api.example.org/accounts/?page=5>; rel="next", <https://api.example.org/accounts/?page=11>; rel="last"
[
{
"id": 1,
"name": "item one",
},
...
]
Requirements
- Python (3.7+)
- Django (3.2+)
- Django REST Framework (3.11+)
Installation
Install using pip:
$ pip install djangorestframework-link-header-pagination
Setup
Add drf_link_header_pagination to your project's INSTALLED_APPS setting.
To enable the LinkHeaderPagination style globally, use the following configuration, modifying the PAGE_SIZE as desired:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'drf_link_header_pagination.LinkHeaderPagination',
'PAGE_SIZE': 100
}
On GenericAPIView subclasses you may also set the pagination_class attribute to select LinkHeaderPagination on a per-view basis.
Other pagination classes that are available are:
LinkHeaderCursorPagination: This is similar to the normalCursorPaginationclass but using theLinkheader to return only thenextand/orprevlinks. Thefirstandlastlinks are unavailable.LinkHeaderLinkResponseCursorPagination: This is similar toLinkHeaderCursorPagination, but in addition to thenextand/orprevURL's being in theLinkheader, the content of the response body is updated to include them as well. The body will be an object with the keysnext(the next page's URL or None),previous(the previous page's URL or None), andresults(the original content of the body).LinkHeaderLimitOffsetPagination: Uses theLimitOffsetPaginationpagination class from DRF to supportoffsetandlimitparameters instead ofpageto indicate offset into the queryset.
Configuration
The configuration is the same as for
PageNumberPagination.
Testing
Use the excellent tox testing tool to run the tests against all supported versions of Python and Django. Install tox globally, and then simply run:
$ tox
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file drf_link_header_pagination-0.2.0.tar.gz.
File metadata
- Download URL: drf_link_header_pagination-0.2.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
19446c5716a8b857f5e0ecbba39a49508a8e748ef51a8cfd6bd706090e97060c
|
|
| MD5 |
086955554d4eb3af6f7516ef0761b043
|
|
| BLAKE2b-256 |
218692c695f5b0f55626dcf2db5d10977d284941b3adceb46d1067a787781382
|
File details
Details for the file drf_link_header_pagination-0.2.0-py3-none-any.whl.
File metadata
- Download URL: drf_link_header_pagination-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9890a871803395544c5a0ee0665fc2d2701fcdf151570ed15c8b8d873aefe27d
|
|
| MD5 |
3b5be684e0d424c7d0cdc30385338628
|
|
| BLAKE2b-256 |
1ab20b73d23049cbe2936e35cf159f0840fca715966e4197d9a6418515d786eb
|