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
This commit is contained in:
Andrew Godwin 2011-01-08 13:15:44 +00:00
parent 19ab52f77f
commit 32a344dfe2
2 changed files with 18 additions and 20 deletions

View File

@ -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):
"""

View File

@ -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)