Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed inconsistency in ListView's pagination (short datasets should a…

…lso 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
  • Loading branch information...
commit 32a344dfe297a16087765d2f4fb2e1398dc21ac8 1 parent 19ab52f
@andrewgodwin andrewgodwin authored
View
29 django/views/generic/list.py
@@ -36,22 +36,19 @@ 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())
- if paginator.num_pages > 1:
- page = self.kwargs.get('page') or self.request.GET.get('page') or 1
@jperelli
jperelli added a note

what is the use of
self.kwargs.get('page')
in this place?
can one define the pagenumber using a url like /view/page/4 instead of /view/?page=4
I can't find out how to do that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
- 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
9 tests/regressiontests/generic_views/list.py
@@ -38,14 +38,15 @@ def test_paginated_queryset(self):
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 @@ def test_paginated_custom_paginator_class(self):
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)
Please sign in to comment.
Something went wrong with that request. Please try again.