Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Changed list_detail.object_list generic view to use the new Paginator…

… class. This is backwards-compatible -- the previous 12 template context variables (has_next, has_previous, etc.) are still passed to the template, but new templates should only access the new template variables 'paginator' and 'page_obj'

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7307 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 08d4910d697d4fb8a199797e0938b0da653d3c3e 1 parent d67208f
Adrian Holovaty authored March 18, 2008

Showing 1 changed file with 27 additions and 23 deletions. Show diff stats Hide diff stats

  1. 50  django/views/generic/list_detail.py
50  django/views/generic/list_detail.py
... ...
@@ -1,7 +1,7 @@
1 1
 from django.template import loader, RequestContext
2 2
 from django.http import Http404, HttpResponse
3 3
 from django.core.xheaders import populate_xheaders
4  
-from django.core.paginator import ObjectPaginator, InvalidPage
  4
+from django.core.paginator import QuerySetPaginator, InvalidPage
5 5
 from django.core.exceptions import ObjectDoesNotExist
6 6
 
7 7
 def object_list(request, queryset, paginate_by=None, page=None,
@@ -45,43 +45,47 @@ def object_list(request, queryset, paginate_by=None, page=None,
45 45
     if extra_context is None: extra_context = {}
46 46
     queryset = queryset._clone()
47 47
     if paginate_by:
48  
-        paginator = ObjectPaginator(queryset, paginate_by)
  48
+        paginator = QuerySetPaginator(queryset, paginate_by, allow_empty_first_page=allow_empty)
49 49
         if not page:
50 50
             page = request.GET.get('page', 1)
51 51
         try:
52 52
             page_number = int(page)
53 53
         except ValueError:
54 54
             if page == 'last':
55  
-                page_number = paginator.pages
  55
+                page_number = paginator.num_pages
56 56
             else:
57  
-                # Page is not 'last', nor can it be converted to an int
  57
+                # Page is not 'last', nor can it be converted to an int.
58 58
                 raise Http404
59 59
         try:
60  
-            object_list = paginator.get_page(page_number - 1)
  60
+            page_obj = paginator.page(page_number)
61 61
         except InvalidPage:
62  
-            if page_number == 1 and allow_empty:
63  
-                object_list = []
64  
-            else:
65  
-                raise Http404
  62
+            raise Http404
66 63
         c = RequestContext(request, {
67  
-            '%s_list' % template_object_name: object_list,
68  
-            'is_paginated': paginator.pages > 1,
69  
-            'results_per_page': paginate_by,
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),
77  
-            'pages': paginator.pages,
78  
-            'hits' : paginator.hits,
79  
-            'page_range' : paginator.page_range
  64
+            '%s_list' % template_object_name: page_obj.object_list,
  65
+            'paginator': paginator,
  66
+            'page_obj': page_obj,
  67
+
  68
+            # Legacy template context stuff. New templates should use page_obj
  69
+            # to access this instead.
  70
+            'is_paginated': page_obj.has_other_pages(),
  71
+            'results_per_page': paginator.per_page,
  72
+            'has_next': page_obj.has_next(),
  73
+            'has_previous': page_obj.has_previous(),
  74
+            'page': page_obj.number,
  75
+            'next': page_obj.next_page_number(),
  76
+            'previous': page_obj.previous_page_number(),
  77
+            'last_on_page': page_obj.start_index(),
  78
+            'first_on_page': page_obj.end_index(),
  79
+            'pages': paginator.num_pages,
  80
+            'hits': paginator.count,
  81
+            'page_range': paginator.page_range,
80 82
         }, context_processors)
81 83
     else:
82 84
         c = RequestContext(request, {
83 85
             '%s_list' % template_object_name: queryset,
84  
-            'is_paginated': False
  86
+            'paginator': None,
  87
+            'page_obj': None,
  88
+            'is_paginated': False,
85 89
         }, context_processors)
86 90
         if not allow_empty and len(queryset) == 0:
87 91
             raise Http404

0 notes on commit 08d4910

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