mirror of https://github.com/django/django.git
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:
parent
19ab52f77f
commit
32a344dfe2
|
@ -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):
|
||||
"""
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue