Ticket #4805: 4805-alt.2.diff
File 4805-alt.2.diff, 5.6 KB (added by , 17 years ago) |
---|
-
django/core/paginator.py
=== modified file 'django/core/paginator.py'
86 86 87 87 hits = property(_get_hits) 88 88 pages = property(_get_pages) 89 90 class ObjectPaginatorForPage(object): 91 """ 92 This class is used to work with a page of a paginator object. 93 Pass a paginator object, and a valid page number. 94 It uses the same methods as a paginator, except you do not need 95 to use a page number, and also provides some other useful 96 properties. 97 98 `page_number` is always 0-based to match the Paginator object. 99 `base` allows next_page_number, previous_page_number and 100 page_number return their numbers using a different base. 101 102 For example: 103 self, query_set, num_per_page, orphans=0 104 >>> paginator = ObjectPaginatorForPage(query_set, num_per_page, 0, base=1) 105 >>> paginator.next_page_number() 106 2 107 >>> paginator.page_number 108 1 109 """ 110 def __init__(self, query_set, num_per_page, page_number=0, base=1, orphans=0): 111 self.paginator = ObjectPaginator(query_set, num_per_page, orphans) 112 # _page_number remains the base-0 page number 113 self._page_number = page_number 114 self.base = base 115 self.num_per_page = num_per_page 116 117 def get_page(self): 118 if not hasattr(self, '_page'): 119 self._page = self.paginator.get_page(self._page_number) 120 return self._page 121 122 def has_next_page(self): 123 return self.paginator.has_next_page(self._page_number) 124 125 def has_previous_page(self): 126 return self.paginator.has_previous_page(self._page_number) 127 128 def first_on_page(self): 129 return self.paginator.first_on_page(self._page_number) 130 131 def last_on_page(self): 132 return self.paginator.last_on_page(self._page_number) 133 134 def _get_hits(self): 135 return self.paginator.hits 136 hits = property(_get_hits) 137 138 def _get_pages(self): 139 return self.paginator.pages 140 pages = property(_get_pages) 141 142 def _get_page_number(self): 143 return self.base + self._page_number 144 page_number = property(_get_page_number) 145 146 def _get_next_page_number(self): 147 if self.has_next_page(): 148 # Uses page_number so that we return the correct base 149 return self.page_number + 1 150 next_page_number = property(_get_next_page_number) 151 152 def _get_previous_page_number(self): 153 if self.has_previous_page(): 154 # Uses page_number so that we return the correct base 155 return self.page_number - 1 156 previous_page_number = property(_get_previous_page_number) -
django/views/generic/list_detail.py
=== modified file 'django/views/generic/list_detail.py'
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 , InvalidPage4 from django.core.paginator import ObjectPaginatorForPage, InvalidPage 5 5 from django.core.exceptions import ObjectDoesNotExist 6 6 7 7 def object_list(request, queryset, paginate_by=None, page=None, … … 17 17 list of objects 18 18 is_paginated 19 19 are the results paginated? 20 results_per_page 21 number of objects per page (if paginated) 22 has_next 23 is there a next page? 24 has_previous 25 is there a prev page? 26 page 27 the current page 28 next 29 the next page 30 previous 31 the previous page 32 pages 33 number of pages, total 34 hits 35 number of objects, total 36 last_on_page 37 the result number of the last of object in the 38 object_list (1-indexed) 39 first_on_page 40 the result number of the first object in the 41 object_list (1-indexed) 20 paginator 21 paginator object used to navigate the list (if paginated) 42 22 """ 43 23 if extra_context is None: extra_context = {} 44 24 queryset = queryset._clone() 45 25 if paginate_by: 46 paginator = ObjectPaginator(queryset, paginate_by)47 26 if not page: 48 27 page = request.GET.get('page', 1) 49 28 try: 50 29 page = int(page) 51 object_list = paginator.get_page(page - 1) 30 paginator = ObjectPaginatorForPage(queryset, paginate_by, page_number=page - 1) 31 object_list = paginator.get_page() 52 32 except (InvalidPage, ValueError): 53 33 if page == 1 and allow_empty: 54 34 object_list = [] 35 paginator = None 55 36 else: 56 37 raise Http404 57 38 c = RequestContext(request, { 58 39 '%s_list' % template_object_name: object_list, 59 'is_paginated': paginator.pages > 1, 60 'results_per_page': paginate_by, 61 'has_next': paginator.has_next_page(page - 1), 62 'has_previous': paginator.has_previous_page(page - 1), 63 'page': page, 64 'next': page + 1, 65 'previous': page - 1, 66 'last_on_page': paginator.last_on_page(page - 1), 67 'first_on_page': paginator.first_on_page(page - 1), 68 'pages': paginator.pages, 69 'hits' : paginator.hits, 40 'is_paginated': paginator and paginator.pages > 1 or False, 70 41 }, context_processors) 42 if paginator is not None: 43 c['paginator'] = paginator 71 44 else: 72 45 c = RequestContext(request, { 73 46 '%s_list' % template_object_name: queryset,