Commit 778b8bdc authored by Jannis Leidel's avatar Jannis Leidel
Browse files

Merge pull request #467 from tomchristie/page-kwarg

Add 'page_kwarg' attribute to `MultipleObjectMixin`, removing hardcoded "page".
parents 73097704 3f2fc2f4
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -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:
+11 −2
Original line number Diff line number Diff line
@@ -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

+10 −0
Original line number Diff line number Diff line
@@ -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/')
+2 −0
Original line number Diff line number Diff line
@@ -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()),