Skip to content
Provide pagination for django-rest-framework using a "Link" HTTP header
Branch: master
Clone or download
Latest commit 0608388 Jul 12, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
drf_link_header_pagination
tests Initial version of drf_link_header_pagination. Jul 12, 2017
.gitignore Initial version of drf_link_header_pagination. Jul 12, 2017
.travis.yml
LICENSE Initial version of drf_link_header_pagination. Jul 12, 2017
MANIFEST.in
README.md Remove support for python3.3. Update travis config. Jul 12, 2017
requirements.txt Initial version of drf_link_header_pagination. Jul 12, 2017
runtests.py Initial version of drf_link_header_pagination. Jul 12, 2017
setup.cfg Initial version of drf_link_header_pagination. Jul 12, 2017
setup.py Remove python3.3 support in setup.py. Jul 12, 2017
tox.ini Remove support for python3.3. Update travis config. Jul 12, 2017

README.md

djangorestframework-link-header-pagination

build-status-image pypi-version

Overview

Provide pagination using a Link HTTP header as described in Github's developer 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 (2.7, 3.4, 3.5, 3.6)
  • Django (1.8, 1.10, 1.11)
  • Django REST Framework (3.6)

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.

Configuration

The configuration is the same as for PageNumberPagination.

Testing

Install testing requirements.

$ pip install -r requirements.txt

Run with runtests.

$ ./runtests.py

You can also 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
You can’t perform that action at this time.