Skip to content

Commit 2a1fd3b

Browse files
tbeadletomchristie
authored andcommitted
Add link to third-party package for LinkHeaderPagination. (encode#5270)
1 parent 8a8389b commit 2a1fd3b

File tree

1 file changed

+5
-23
lines changed

1 file changed

+5
-23
lines changed

docs/api-guide/pagination.md

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -242,29 +242,6 @@ We'd then need to setup the custom class in our configuration:
242242

243243
Note that if you care about how the ordering of keys is displayed in responses in the browsable API you might choose to use an `OrderedDict` when constructing the body of paginated responses, but this is optional.
244244

245-
## Header based pagination
246-
247-
Let's modify the built-in `PageNumberPagination` style, so that instead of include the pagination links in the body of the response, we'll instead include a `Link` header, in a [similar style to the GitHub API][github-link-pagination].
248-
249-
class LinkHeaderPagination(pagination.PageNumberPagination):
250-
def get_paginated_response(self, data):
251-
next_url = self.get_next_link()
252-
previous_url = self.get_previous_link()
253-
254-
if next_url is not None and previous_url is not None:
255-
link = '<{next_url}>; rel="next", <{previous_url}>; rel="prev"'
256-
elif next_url is not None:
257-
link = '<{next_url}>; rel="next"'
258-
elif previous_url is not None:
259-
link = '<{previous_url}>; rel="prev"'
260-
else:
261-
link = ''
262-
263-
link = link.format(next_url=next_url, previous_url=previous_url)
264-
headers = {'Link': link} if link else {}
265-
266-
return Response(data, headers=headers)
267-
268245
## Using your custom pagination class
269246

270247
To have your custom pagination class be used by default, use the `DEFAULT_PAGINATION_CLASS` setting:
@@ -328,10 +305,15 @@ The [`DRF-extensions` package][drf-extensions] includes a [`PaginateByMaxMixin`
328305

329306
The [`drf-proxy-pagination` package][drf-proxy-pagination] includes a `ProxyPagination` class which allows to choose pagination class with a query parameter.
330307

308+
## link-header-pagination
309+
310+
The [`django-rest-framework-link-header-pagination` package][drf-link-header-pagination] includes a `LinkHeaderPagination` class which provides pagination via an HTTP `Link` header as desribed in [Github's developer documentation](github-link-pagination).
311+
331312
[cite]: https://docs.djangoproject.com/en/stable/topics/pagination/
332313
[github-link-pagination]: https://developer.github.com/guides/traversing-with-pagination/
333314
[link-header]: ../img/link-header-pagination.png
334315
[drf-extensions]: http://chibisov.github.io/drf-extensions/docs/
335316
[paginate-by-max-mixin]: http://chibisov.github.io/drf-extensions/docs/#paginatebymaxmixin
336317
[drf-proxy-pagination]: https://github.com/tuffnatty/drf-proxy-pagination
318+
[drf-link-header-pagination]: https://github.com/tbeadle/django-rest-framework-link-header-pagination
337319
[disqus-cursor-api]: http://cramer.io/2011/03/08/building-cursors-for-the-disqus-api

0 commit comments

Comments
 (0)