From 32a344dfe297a16087765d2f4fb2e1398dc21ac8 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Sat, 8 Jan 2011 13:15:44 +0000 Subject: [PATCH] Fixed inconsistency in ListView's pagination (short datasets should also trigger a pagination, but with a single possible page) git-svn-id: http://code.djangoproject.com/svn/django/trunk@15155 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/views/generic/list.py | 29 +++++++++------------ tests/regressiontests/generic_views/list.py | 9 ++++--- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/django/views/generic/list.py b/django/views/generic/list.py index cd7f9f6ae8e..7b817a5f021 100644 --- a/django/views/generic/list.py +++ b/django/views/generic/list.py @@ -36,22 +36,19 @@ class MultipleObjectMixin(object): Paginate the queryset, if needed. """ paginator = self.get_paginator(queryset, page_size, allow_empty_first_page=self.get_allow_empty()) - if paginator.num_pages > 1: - page = self.kwargs.get('page') or self.request.GET.get('page') or 1 - try: - page_number = int(page) - except ValueError: - if page == 'last': - page_number = paginator.num_pages - else: - raise Http404("Page is not 'last', nor can it be converted to an int.") - try: - page = paginator.page(page_number) - return (paginator, page, page.object_list, True) - except InvalidPage: - raise Http404(u'Invalid page (%s)' % page_number) - else: - return (None, None, queryset, False) + page = self.kwargs.get('page') or self.request.GET.get('page') or 1 + try: + page_number = int(page) + except ValueError: + if page == 'last': + page_number = paginator.num_pages + else: + raise Http404("Page is not 'last', nor can it be converted to an int.") + try: + page = paginator.page(page_number) + return (paginator, page, page.object_list, True) + except InvalidPage: + raise Http404(u'Invalid page (%s)' % page_number) def get_paginate_by(self, queryset): """ diff --git a/tests/regressiontests/generic_views/list.py b/tests/regressiontests/generic_views/list.py index 7414c62d3a2..de7302048b1 100644 --- a/tests/regressiontests/generic_views/list.py +++ b/tests/regressiontests/generic_views/list.py @@ -38,14 +38,15 @@ class ListViewTests(TestCase): self.assertEqual(list(res.context['authors'])[-1].name, 'Author 29') def test_paginated_queryset_shortdata(self): + # Test that short datasets ALSO result in a paginated view. res = self.client.get('/list/authors/paginated/') self.assertEqual(res.status_code, 200) self.assertTemplateUsed(res, 'generic_views/author_list.html') self.assertEqual(list(res.context['object_list']), list(Author.objects.all())) self.assertIs(res.context['authors'], res.context['object_list']) - self.assertIsNone(res.context['paginator']) - self.assertIsNone(res.context['page_obj']) - self.assertFalse(res.context['is_paginated']) + self.assertEqual(res.context['page_obj'].number, 1) + self.assertEqual(res.context['paginator'].num_pages, 1) + self.assertTrue(res.context['is_paginated']) def test_paginated_get_page_by_query_string(self): self._make_authors(100) @@ -90,7 +91,7 @@ class ListViewTests(TestCase): self._make_authors(7) res = self.client.get('/list/authors/paginated/custom_class/') self.assertEqual(res.status_code, 200) - self.assertIsNone(res.context['paginator']) + self.assertEqual(res.context['paginator'].num_pages, 1) # Custom pagination allows for 2 orphans on a page size of 5 self.assertEqual(len(res.context['object_list']), 7)