Ticket #2574: patch.diff
File patch.diff, 4.9 KB (added by , 18 years ago) |
---|
-
django/core/paginator.py
72 72 elif page_number == (self._pages - 1) and (page_number + 1) * self.num_per_page > self._hits: 73 73 return self._hits 74 74 return (page_number + 1) * self.num_per_page 75 76 def page_list(self, page, page_list_surround, page_list_edges): 77 page_list = [] 78 if page_list_surround: 79 surround_start = page-page_list_surround 80 surround_end = (page+page_list_surround)+1 81 if page_list_edges: 82 page_list.extend(range(1, page_list_edges+1)) 83 if page_list[-1] < surround_start: 84 page_list.append("sep") 85 if surround_start <= 1: 86 surround_start = 1 87 else: 88 page_list.append("sep") 89 if surround_end > self.pages-page_list_edges: 90 surround_end = (self.pages-page_list_edges)+1 91 page_list.extend(range(surround_start, surround_end)) 92 if page_list_edges: 93 if not self.pages-page_list_edges == surround_end: 94 page_list.append("sep") 95 page_list.extend(range(self.pages-page_list_edges, self.pages)) 96 elif surround_end-1 != self.pages: 97 page_list.append("sep") 98 else: 99 page_list.extend(range(1, self.pages+1)) 100 return page_list 75 101 76 102 def _get_hits(self): 77 103 if self._hits is None: 78 self._hits = self.query_set.count() 104 try: 105 self._hits = self.query_set.count() 106 except TypeError: 107 self._hits = len(self.query_set) 79 108 return self._hits 80 109 81 110 def _get_pages(self): -
django/views/generic/list_detail.py
5 5 from django.core.exceptions import ObjectDoesNotExist 6 6 7 7 def object_list(request, queryset, paginate_by=None, page=None, 8 allow_empty=False, template_name=None, template_loader=loader, 9 extra_context=None, context_processors=None, template_object_name='object', 8 allow_empty=False, page_list_surround=0, page_list_edges=0, 9 template_name=None, template_loader=loader, extra_context=None, 10 context_processors=None, template_object_name='object', 10 11 mimetype=None): 11 12 """ 12 13 Generic list of objects. … … 33 34 number of pages, total 34 35 hits 35 36 number of objects, total 37 page_list 38 a list of containing the page numbers surrounding 39 the current page and the page numbers at the edge 40 (begining and end) depending on the arguments 41 passed 42 first_on_page 43 the result number of the first object in the 44 object_list (where the objects are numbered 45 starting at 1) 46 last_on_page 47 the result number of the last of object in the 48 object_list (where the objects are numbered 49 starting at 1) 36 50 """ 37 if extra_context is None: extra_context = {} 38 queryset = queryset._clone() 51 if extra_context is None: 52 extra_context = {} 53 if type(queryset) != type([]): 54 queryset = queryset._clone() #needs to be sequence 39 55 if paginate_by: 40 56 paginator = ObjectPaginator(queryset, paginate_by) 41 57 if not page: … … 59 75 'previous': page - 1, 60 76 'pages': paginator.pages, 61 77 'hits' : paginator.hits, 78 'first_on_page': paginator.first_on_page(page - 1), 79 'last_on_page': paginator.last_on_page(page - 1), 80 'page_list': paginator.page_list(page, page_list_surround, page_list_edges), 62 81 }, context_processors) 63 82 else: 64 83 c = RequestContext(request, { … … 67 86 }, context_processors) 68 87 if not allow_empty and len(queryset) == 0: 69 88 raise Http404 89 70 90 for key, value in extra_context.items(): 71 91 if callable(value): 72 92 c[key] = value() 73 93 else: 74 94 c[key] = value 75 95 if not template_name: 76 model = queryset.model 77 template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower()) 96 if type(queryset) == type([]): 97 raise AttributeError, "Generic list view must be called with template_name when queryset is a list." 98 else: 99 model = queryset.model 100 template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower()) 78 101 t = template_loader.get_template(template_name) 79 102 return HttpResponse(t.render(c), mimetype=mimetype) 80 103