From 502be865c68635d5c31fa3fa58162b48412153ad Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 25 Oct 2012 10:31:14 +0100 Subject: [PATCH 1/4] Add 'page_kwarg' attribute to `MultipleObjectMixin`, removing hardcoded 'page'. --- django/views/generic/list.py | 4 +++- docs/ref/class-based-views/mixins-multiple-object.txt | 11 +++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/django/views/generic/list.py b/django/views/generic/list.py index ec30c58f29..0c383d609a 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..e6abf26e6a 100644 --- a/docs/ref/class-based-views/mixins-multiple-object.txt +++ b/docs/ref/class-based-views/mixins-multiple-object.txt @@ -69,8 +69,15 @@ 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 + + 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 From f824a951776db86c04aaefc1e7c1c12ffb84c798 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 25 Oct 2012 13:19:53 +0100 Subject: [PATCH 2/4] Test for `ListView.page_kwarg` --- tests/regressiontests/generic_views/list.py | 10 ++++++++++ tests/regressiontests/generic_views/urls.py | 2 ++ 2 files changed, 12 insertions(+) diff --git a/tests/regressiontests/generic_views/list.py b/tests/regressiontests/generic_views/list.py index 14dc1d725d..d267824b84 100644 --- a/tests/regressiontests/generic_views/list.py +++ b/tests/regressiontests/generic_views/list.py @@ -99,6 +99,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()), From 5bc6929f9af4d71065bce42578e49354096a7bf4 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 9 Nov 2012 16:15:19 +0000 Subject: [PATCH 3/4] Include `versionadded 1.5` directive --- docs/ref/class-based-views/mixins-multiple-object.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/ref/class-based-views/mixins-multiple-object.txt b/docs/ref/class-based-views/mixins-multiple-object.txt index e6abf26e6a..bdcb43b163 100644 --- a/docs/ref/class-based-views/mixins-multiple-object.txt +++ b/docs/ref/class-based-views/mixins-multiple-object.txt @@ -74,6 +74,8 @@ MultipleObjectMixin .. 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 From 3f2fc2f41abf226913517eb1e655f823f2c5e53a Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 9 Nov 2012 16:16:58 +0000 Subject: [PATCH 4/4] Formatting tweaks. --- docs/ref/class-based-views/mixins-multiple-object.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ref/class-based-views/mixins-multiple-object.txt b/docs/ref/class-based-views/mixins-multiple-object.txt index bdcb43b163..fb5a1715e6 100644 --- a/docs/ref/class-based-views/mixins-multiple-object.txt +++ b/docs/ref/class-based-views/mixins-multiple-object.txt @@ -79,7 +79,7 @@ MultipleObjectMixin 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"``. + in the URLconf. Defaults to ``page``. .. attribute:: paginator_class