Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add 'page_kwarg' attribute to `MultipleObjectMixin`, removing hardcoded "page" #467

Merged
merged 4 commits into from

5 participants

@tomchristie

At the moment it's awkward to modify the generic views to use a custom name for the 'page' query parameter.

Includes documentation.

@mjtamlyn
Collaborator

+1, needs a test as well though

@apollo13
Owner

We don't accept pull requests without the associated ticket beeing accepted. Is there a ticket? If yes please link it, if no, open one. Thx!

@mjtamlyn
Collaborator

Marked as RFC!

@tomchristie

Any chance of this making it in before the beta freeze?
Don't think there's anything contentious here, and the patch, tests and docs should cover everything.

@apollo13 apollo13 commented on the diff
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
@apollo13 Owner
apollo13 added a note

Misses a "versionadded 1.5" directive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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"``.
@apollo13 Owner
apollo13 added a note

There are two blanks, remove one. Also remove the " around page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jezdez jezdez merged commit 778b8bd into django:master
@SmileyChris
Collaborator

@jezdez, shouldn't this be pulled into 1.5.X if it has been marked as versionadded 1.5 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
4 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 @@ def paginate_queryset(self, queryset, page_size):
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:
View
13 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
@apollo13 Owner
apollo13 added a note

Misses a "versionadded 1.5" directive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ .. 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
View
10 tests/regressiontests/generic_views/list.py
@@ -99,6 +99,16 @@ def test_paginated_custom_paginator_class(self):
# 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/')
View
2  tests/regressiontests/generic_views/urls.py
@@ -149,6 +149,8 @@
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()),
Something went wrong with that request. Please try again.