Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Andrew Godwin authored January 08, 2011
29  django/views/generic/list.py
@@ -36,22 +36,19 @@ def paginate_queryset(self, queryset, page_size):
36 36
         Paginate the queryset, if needed.
37 37
         """
38 38
         paginator = self.get_paginator(queryset, page_size, allow_empty_first_page=self.get_allow_empty())
39  
-        if paginator.num_pages > 1:
40  
-            page = self.kwargs.get('page') or self.request.GET.get('page') or 1
41  
-            try:
42  
-                page_number = int(page)
43  
-            except ValueError:
44  
-                if page == 'last':
45  
-                    page_number = paginator.num_pages
46  
-                else:
47  
-                    raise Http404("Page is not 'last', nor can it be converted to an int.")
48  
-            try:
49  
-                page = paginator.page(page_number)
50  
-                return (paginator, page, page.object_list, True)
51  
-            except InvalidPage:
52  
-                raise Http404(u'Invalid page (%s)' % page_number)
53  
-        else:
54  
-            return (None, None, queryset, False)
  39
+        page = self.kwargs.get('page') or self.request.GET.get('page') or 1
  40
+        try:
  41
+            page_number = int(page)
  42
+        except ValueError:
  43
+            if page == 'last':
  44
+                page_number = paginator.num_pages
  45
+            else:
  46
+                raise Http404("Page is not 'last', nor can it be converted to an int.")
  47
+        try:
  48
+            page = paginator.page(page_number)
  49
+            return (paginator, page, page.object_list, True)
  50
+        except InvalidPage:
  51
+            raise Http404(u'Invalid page (%s)' % page_number)
55 52
 
56 53
     def get_paginate_by(self, queryset):
57 54
         """
9  tests/regressiontests/generic_views/list.py
@@ -38,14 +38,15 @@ def test_paginated_queryset(self):
38 38
         self.assertEqual(list(res.context['authors'])[-1].name, 'Author 29')
39 39
 
40 40
     def test_paginated_queryset_shortdata(self):
  41
+        # Test that short datasets ALSO result in a paginated view.
41 42
         res = self.client.get('/list/authors/paginated/')
42 43
         self.assertEqual(res.status_code, 200)
43 44
         self.assertTemplateUsed(res, 'generic_views/author_list.html')
44 45
         self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
45 46
         self.assertIs(res.context['authors'], res.context['object_list'])
46  
-        self.assertIsNone(res.context['paginator'])
47  
-        self.assertIsNone(res.context['page_obj'])
48  
-        self.assertFalse(res.context['is_paginated'])
  47
+        self.assertEqual(res.context['page_obj'].number, 1)
  48
+        self.assertEqual(res.context['paginator'].num_pages, 1)
  49
+        self.assertTrue(res.context['is_paginated'])
49 50
 
50 51
     def test_paginated_get_page_by_query_string(self):
51 52
         self._make_authors(100)
@@ -90,7 +91,7 @@ def test_paginated_custom_paginator_class(self):
90 91
         self._make_authors(7)
91 92
         res = self.client.get('/list/authors/paginated/custom_class/')
92 93
         self.assertEqual(res.status_code, 200)
93  
-        self.assertIsNone(res.context['paginator'])
  94
+        self.assertEqual(res.context['paginator'].num_pages, 1)
94 95
         # Custom pagination allows for 2 orphans on a page size of 5
95 96
         self.assertEqual(len(res.context['object_list']), 7)
96 97
 

0 notes on commit 32a344d

jperelli

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

Please sign in to comment.
Something went wrong with that request. Please try again.