diff --git a/django/views/generic/list.py b/django/views/generic/list.py index e9d57a0702..b316c4bb56 100644 --- a/django/views/generic/list.py +++ b/django/views/generic/list.py @@ -17,6 +17,7 @@ class MultipleObjectMixin(ContextMixin): paginate_by = None context_object_name = None paginator_class = Paginator + page_kwarg = 'page' def get_queryset(self): """ @@ -39,7 +40,8 @@ class MultipleObjectMixin(ContextMixin): Paginate the queryset, if needed. """ 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: page_number = int(page) except ValueError: diff --git a/docs/ref/class-based-views/mixins-multiple-object.txt b/docs/ref/class-based-views/mixins-multiple-object.txt index cdb743fcbd..fb5a1715e6 100644 --- a/docs/ref/class-based-views/mixins-multiple-object.txt +++ b/docs/ref/class-based-views/mixins-multiple-object.txt @@ -69,8 +69,17 @@ MultipleObjectMixin An integer specifying how many objects should be displayed per page. If this is given, the view will paginate objects with :attr:`MultipleObjectMixin.paginate_by` objects per page. The view will - expect either a ``page`` query string parameter (via ``GET``) or a - ``page`` variable specified in the URLconf. + expect either a ``page`` query string parameter (via ``request.GET``) + 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 diff --git a/tests/regressiontests/generic_views/list.py b/tests/regressiontests/generic_views/list.py index a8ed9e856b..28d37f0e71 100644 --- a/tests/regressiontests/generic_views/list.py +++ b/tests/regressiontests/generic_views/list.py @@ -101,6 +101,16 @@ class ListViewTests(TestCase): # Custom pagination allows for 2 orphans on a page size of 5 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): self._make_authors(7) res = self.client.get('/list/authors/paginated/custom_constructor/') diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py index c72bfecb65..a212b830a5 100644 --- a/tests/regressiontests/generic_views/urls.py +++ b/tests/regressiontests/generic_views/urls.py @@ -149,6 +149,8 @@ urlpatterns = patterns('', views.AuthorList.as_view(queryset=None)), (r'^list/authors/paginated/custom_class/$', 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/$', views.AuthorListCustomPaginator.as_view()),