Merge pull request #467 from tomchristie/page-kwarg

Add 'page_kwarg' attribute to `MultipleObjectMixin`, removing hardcoded "page".
This commit is contained in:
Jannis Leidel 2012-11-17 12:27:01 -08:00
commit 778b8bdcf4
4 changed files with 26 additions and 3 deletions

View File

@ -17,6 +17,7 @@ class MultipleObjectMixin(ContextMixin):
paginate_by = None paginate_by = None
context_object_name = None context_object_name = None
paginator_class = Paginator paginator_class = Paginator
page_kwarg = 'page'
def get_queryset(self): def get_queryset(self):
""" """
@ -39,7 +40,8 @@ class MultipleObjectMixin(ContextMixin):
Paginate the queryset, if needed. Paginate the queryset, if needed.
""" """
paginator = self.get_paginator(queryset, page_size, allow_empty_first_page=self.get_allow_empty()) paginator = self.get_paginator(queryset, page_size, allow_empty_first_page=self.get_allow_empty())
page = self.kwargs.get('page') or self.request.GET.get('page') or 1 page_kwarg = self.page_kwarg
page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg) or 1
try: try:
page_number = int(page) page_number = int(page)
except ValueError: except ValueError:

View File

@ -69,8 +69,17 @@ MultipleObjectMixin
An integer specifying how many objects should be displayed per page. If An integer specifying how many objects should be displayed per page. If
this is given, the view will paginate objects with this is given, the view will paginate objects with
:attr:`MultipleObjectMixin.paginate_by` objects per page. The view will :attr:`MultipleObjectMixin.paginate_by` objects per page. The view will
expect either a ``page`` query string parameter (via ``GET``) or a expect either a ``page`` query string parameter (via ``request.GET``)
``page`` variable specified in the URLconf. or a ``page`` variable specified in the URLconf.
.. attribute:: page_kwarg
.. versionadded:: 1.5
A string specifying the name to use for the page parameter.
The view will expect this prameter to be available either as a query
string parameter (via ``request.GET``) or as a kwarg variable specified
in the URLconf. Defaults to ``page``.
.. attribute:: paginator_class .. attribute:: paginator_class

View File

@ -101,6 +101,16 @@ class ListViewTests(TestCase):
# Custom pagination allows for 2 orphans on a page size of 5 # Custom pagination allows for 2 orphans on a page size of 5
self.assertEqual(len(res.context['object_list']), 7) self.assertEqual(len(res.context['object_list']), 7)
def test_paginated_custom_page_kwarg(self):
self._make_authors(100)
res = self.client.get('/list/authors/paginated/custom_page_kwarg/', {'pagina': '2'})
self.assertEqual(res.status_code, 200)
self.assertTemplateUsed(res, 'generic_views/author_list.html')
self.assertEqual(len(res.context['object_list']), 30)
self.assertIs(res.context['author_list'], res.context['object_list'])
self.assertEqual(res.context['author_list'][0].name, 'Author 30')
self.assertEqual(res.context['page_obj'].number, 2)
def test_paginated_custom_paginator_constructor(self): def test_paginated_custom_paginator_constructor(self):
self._make_authors(7) self._make_authors(7)
res = self.client.get('/list/authors/paginated/custom_constructor/') res = self.client.get('/list/authors/paginated/custom_constructor/')

View File

@ -149,6 +149,8 @@ urlpatterns = patterns('',
views.AuthorList.as_view(queryset=None)), views.AuthorList.as_view(queryset=None)),
(r'^list/authors/paginated/custom_class/$', (r'^list/authors/paginated/custom_class/$',
views.AuthorList.as_view(paginate_by=5, paginator_class=views.CustomPaginator)), views.AuthorList.as_view(paginate_by=5, paginator_class=views.CustomPaginator)),
(r'^list/authors/paginated/custom_page_kwarg/$',
views.AuthorList.as_view(paginate_by=30, page_kwarg='pagina')),
(r'^list/authors/paginated/custom_constructor/$', (r'^list/authors/paginated/custom_constructor/$',
views.AuthorListCustomPaginator.as_view()), views.AuthorListCustomPaginator.as_view()),