mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
Tweaks
This commit is contained in:
parent
3833a5bb8a
commit
313aa727e3
|
@ -74,7 +74,7 @@ Note that the `paginate_queryset` method may set state on the pagination instanc
|
|||
|
||||
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].
|
||||
|
||||
class LinkHeaderPagination(PageNumberPagination)
|
||||
class LinkHeaderPagination(pagination.PageNumberPagination):
|
||||
def get_paginated_response(self, data):
|
||||
next_url = self.get_next_link()
previous_url = self.get_previous_link()
|
||||
|
||||
|
@ -82,7 +82,7 @@ Let's modify the built-in `PageNumberPagination` style, so that instead of inclu
|
|||
link = '<{next_url}; rel="next">, <{previous_url}; rel="prev">'
|
||||
elif next_url is not None:
|
||||
link = '<{next_url}; rel="next">'
|
||||
elif prev_url is not None:
|
||||
elif previous_url is not None:
|
||||
link = '<{previous_url}; rel="prev">'
|
||||
else:
|
||||
link = ''
|
||||
|
@ -97,10 +97,20 @@ Let's modify the built-in `PageNumberPagination` style, so that instead of inclu
|
|||
To have your custom pagination class be used by default, use the `DEFAULT_PAGINATION_CLASS` setting:
|
||||
|
||||
REST_FRAMEWORK = {
|
||||
'DEFAULT_PAGINATION_CLASS':
|
||||
'my_project.apps.core.pagination.LinkHeaderPagination',
|
||||
'DEFAULT_PAGINATION_CLASS': 'my_project.apps.core.pagination.LinkHeaderPagination',
|
||||
'PAGINATE_BY': 10
|
||||
}
|
||||
|
||||
API responses for list endpoints will now include a `Link` header, instead of including the pagination links as part of the body of the response, for example:
|
||||
|
||||
---
|
||||
|
||||
![Link Header][link-header]
|
||||
|
||||
*A custom pagination style, using the 'Link' header'*
|
||||
|
||||
---
|
||||
|
||||
# Third party packages
|
||||
|
||||
The following third party packages are also available.
|
||||
|
@ -111,5 +121,6 @@ The [`DRF-extensions` package][drf-extensions] includes a [`PaginateByMaxMixin`
|
|||
|
||||
[cite]: https://docs.djangoproject.com/en/dev/topics/pagination/
|
||||
[github-link-pagination]: https://developer.github.com/guides/traversing-with-pagination/
|
||||
[link-header]: ../img/link-header-pagination.png
|
||||
[drf-extensions]: http://chibisov.github.io/drf-extensions/docs/
|
||||
[paginate-by-max-mixin]: http://chibisov.github.io/drf-extensions/docs/#paginatebymaxmixin
|
||||
|
|
BIN
docs/img/link-header-pagination.png
Normal file
BIN
docs/img/link-header-pagination.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 35 KiB |
|
@ -46,6 +46,12 @@ def _get_displayed_page_numbers(current, final):
|
|||
|
||||
For example:
|
||||
current=14, final=16 -> [1, None, 13, 14, 15, 16]
|
||||
|
||||
This implementation gives one page to each side of the cursor,
|
||||
for an implementation which gives two pages to each side of the cursor,
|
||||
which is a copy of how GitHub treat pagination in their issue lists, see:
|
||||
|
||||
https://gist.github.com/tomchristie/321140cebb1c4a558b15
|
||||
"""
|
||||
assert current >= 1
|
||||
assert final >= current
|
||||
|
@ -60,10 +66,12 @@ def _get_displayed_page_numbers(current, final):
|
|||
|
||||
# If the break would only exclude a single page number then we
|
||||
# may as well include the page number instead of the break.
|
||||
if current == 4:
|
||||
if current <= 4:
|
||||
included.add(2)
|
||||
if current == final - 3:
|
||||
included.add(3)
|
||||
if current >= final - 3:
|
||||
included.add(final - 1)
|
||||
included.add(final - 2)
|
||||
|
||||
# Now sort the page numbers and drop anything outside the limits.
|
||||
included = [
|
||||
|
|
Loading…
Reference in New Issue
Block a user