Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4919 -- Added 'last' marker on paginators. Thanks to patrick@v…

…onautomatisch.atfor the idea, and nick@efford.org for the patch and docs.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6149 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d14c756b5e30c73fcb973905b67f091169d33595 1 parent 62fe5cf
Russell Keith-Magee authored September 14, 2007
29  django/views/generic/list_detail.py
@@ -49,10 +49,17 @@ def object_list(request, queryset, paginate_by=None, page=None,
49 49
         if not page:
50 50
             page = request.GET.get('page', 1)
51 51
         try:
52  
-            page = int(page)
53  
-            object_list = paginator.get_page(page - 1)
54  
-        except (InvalidPage, ValueError):
55  
-            if page == 1 and allow_empty:
  52
+            page_number = int(page)
  53
+        except ValueError:
  54
+            if page == 'last':
  55
+                page_number = paginator.pages
  56
+            else:
  57
+                # Page is not 'last', nor can it be converted to an int
  58
+                raise Http404
  59
+        try:
  60
+            object_list = paginator.get_page(page_number - 1)
  61
+        except InvalidPage:
  62
+            if page_number == 1 and allow_empty:
56 63
                 object_list = []
57 64
             else:
58 65
                 raise Http404
@@ -60,13 +67,13 @@ def object_list(request, queryset, paginate_by=None, page=None,
60 67
             '%s_list' % template_object_name: object_list,
61 68
             'is_paginated': paginator.pages > 1,
62 69
             'results_per_page': paginate_by,
63  
-            'has_next': paginator.has_next_page(page - 1),
64  
-            'has_previous': paginator.has_previous_page(page - 1),
65  
-            'page': page,
66  
-            'next': page + 1,
67  
-            'previous': page - 1,
68  
-            'last_on_page': paginator.last_on_page(page - 1),
69  
-            'first_on_page': paginator.first_on_page(page - 1),
  70
+            'has_next': paginator.has_next_page(page_number - 1),
  71
+            'has_previous': paginator.has_previous_page(page_number - 1),
  72
+            'page': page_number,
  73
+            'next': page_number + 1,
  74
+            'previous': page_number - 1,
  75
+            'last_on_page': paginator.last_on_page(page_number - 1),
  76
+            'first_on_page': paginator.first_on_page(page_number - 1),
70 77
             'pages': paginator.pages,
71 78
             'hits' : paginator.hits,
72 79
             'page_range' : paginator.page_range
18  docs/generic_views.txt
@@ -688,9 +688,8 @@ A page representing a list of objects.
688 688
     * ``paginate_by``: An integer specifying how many objects should be
689 689
       displayed per page. If this is given, the view will paginate objects with
690 690
       ``paginate_by`` objects per page. The view will expect either a ``page``
691  
-      query string parameter (via ``GET``) containing a 1-based page
692  
-      number, or a ``page`` variable specified in the URLconf. See
693  
-      "Notes on pagination" below.
  691
+      query string parameter (via ``GET``) or a ``page`` variable specified in
  692
+      the URLconf. See "Notes on pagination" below.
694 693
 
695 694
     * ``template_name``: The full name of a template to use in rendering the
696 695
       page. This lets you override the default template name (see below).
@@ -780,7 +779,7 @@ specify the page number in the URL in one of two ways:
780 779
         (r'^objects/page(?P<page>[0-9]+)/$', 'object_list', dict(info_dict))
781 780
 
782 781
     * Pass the page number via the ``page`` query-string parameter. For
783  
-      example, a URL would look like this:
  782
+      example, a URL would look like this::
784 783
 
785 784
         /objects/?page=3
786 785
 
@@ -789,7 +788,16 @@ specify the page number in the URL in one of two ways:
789 788
       to create a link to every page of results.
790 789
 
791 790
 These values and lists are is 1-based, not 0-based, so the first page would be
792  
-represented as page ``1``.
  791
+represented as page ``1``. As a special case, you are also permitted to use 
  792
+``last`` as a value for ``page``::
  793
+
  794
+    /objects/?page=last
  795
+
  796
+This allows you to access the final page of results without first having to 
  797
+determine how many pages there are.
  798
+
  799
+Note that ``page`` *must* be either a valid page number or the value ``last``;
  800
+any other value for ``page`` will result in a 404 error.
793 801
 
794 802
 ``django.views.generic.list_detail.object_detail``
795 803
 --------------------------------------------------

0 notes on commit d14c756

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