class PageNumberPagination
from rest_framework.pagination import PageNumberPagination
Documentation
Source code
A simple page number based style that supports page numbers as query parameters. For example: http://api.example.org/accounts/?page=4 http://api.example.org/accounts/?page=4&page_size=100
Ancestors (MRO)
- PageNumberPagination
- BasePagination
Attributes
Defined in | |
---|---|
display_page_controls = False
|
BasePagination |
django_paginator_class = <class 'django.core.paginator.Paginator'>
|
PageNumberPagination |
invalid_page_message = 'Invalid page.'
|
PageNumberPagination |
last_page_strings = ('last',)
|
PageNumberPagination |
max_page_size = None
|
PageNumberPagination |
page_query_description = 'A page number within the paginated result set.'
|
PageNumberPagination |
page_query_param = 'page'
|
PageNumberPagination |
page_size = None
|
PageNumberPagination |
page_size_query_description = 'Number of results to return per page.'
|
PageNumberPagination |
page_size_query_param = None
|
PageNumberPagination |
template = 'rest_framework/pagination/numbers.html'
|
PageNumberPagination |
Methods
def
get_html_context(self):
¶
PageNumberPagination
def get_html_context(self):
base_url = self.request.build_absolute_uri()
def page_number_to_url(page_number):
if page_number == 1:
return remove_query_param(base_url, self.page_query_param)
else:
return replace_query_param(base_url, self.page_query_param, page_number)
current = self.page.number
final = self.page.paginator.num_pages
page_numbers = _get_displayed_page_numbers(current, final)
page_links = _get_page_links(page_numbers, current, page_number_to_url)
return {
'previous_url': self.get_previous_link(),
'next_url': self.get_next_link(),
'page_links': page_links
}
def
get_next_link(self):
¶
PageNumberPagination
def get_next_link(self):
if not self.page.has_next():
return None
url = self.request.build_absolute_uri()
page_number = self.page.next_page_number()
return replace_query_param(url, self.page_query_param, page_number)
def
get_page_size(self, request):
¶
PageNumberPagination
def get_page_size(self, request):
if self.page_size_query_param:
try:
return _positive_int(
request.query_params[self.page_size_query_param],
strict=True,
cutoff=self.max_page_size
)
except (KeyError, ValueError):
pass
return self.page_size
def
get_paginated_response(self, data):
¶
PageNumberPagination
def get_paginated_response(self, data):
return Response(OrderedDict([
('count', self.page.paginator.count),
('next', self.get_next_link()),
('previous', self.get_previous_link()),
('results', data)
]))
BasePagination
def get_paginated_response(self, data): # pragma: no cover
raise NotImplementedError('get_paginated_response() must be implemented.')
def
get_previous_link(self):
¶
PageNumberPagination
def get_previous_link(self):
if not self.page.has_previous():
return None
url = self.request.build_absolute_uri()
page_number = self.page.previous_page_number()
if page_number == 1:
return remove_query_param(url, self.page_query_param)
return replace_query_param(url, self.page_query_param, page_number)
def
get_results(self, data):
¶
BasePagination
def get_results(self, data):
return data['results']
def
get_schema_fields(self, view):
¶
PageNumberPagination
def get_schema_fields(self, view):
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
fields = [
coreapi.Field(
name=self.page_query_param,
required=False,
location='query',
schema=coreschema.Integer(
title='Page',
description=force_text(self.page_query_description)
)
)
]
if self.page_size_query_param is not None:
fields.append(
coreapi.Field(
name=self.page_size_query_param,
required=False,
location='query',
schema=coreschema.Integer(
title='Page size',
description=force_text(self.page_size_query_description)
)
)
)
return fields
BasePagination
def get_schema_fields(self, view):
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
return []
def
paginate_queryset(self, queryset, request, view=None):
¶
PageNumberPagination
Paginate a queryset if required, either returning a page object, or `None` if pagination is not configured for this view.
def paginate_queryset(self, queryset, request, view=None):
"""
Paginate a queryset if required, either returning a
page object, or `None` if pagination is not configured for this view.
"""
page_size = self.get_page_size(request)
if not page_size:
return None
paginator = self.django_paginator_class(queryset, page_size)
page_number = request.query_params.get(self.page_query_param, 1)
if page_number in self.last_page_strings:
page_number = paginator.num_pages
try:
self.page = paginator.page(page_number)
except InvalidPage as exc:
msg = self.invalid_page_message.format(
page_number=page_number, message=six.text_type(exc)
)
raise NotFound(msg)
if paginator.num_pages > 1 and self.template is not None:
# The browsable API should display pagination controls.
self.display_page_controls = True
self.request = request
return list(self.page)
BasePagination
def paginate_queryset(self, queryset, request, view=None): # pragma: no cover
raise NotImplementedError('paginate_queryset() must be implemented.')
def
to_html(self):
¶
PageNumberPagination
def to_html(self):
template = loader.get_template(self.template)
context = self.get_html_context()
return template.render(context)
BasePagination
def to_html(self): # pragma: no cover
raise NotImplementedError('to_html() must be implemented to display page controls.')