Ticket #2574: patch.diff

File patch.diff, 4.9 KB (added by grimboy, 9 years ago)

Oops - this is the patch in the correct diff format

  • django/core/paginator.py

     
    7272        elif page_number == (self._pages - 1) and (page_number + 1) * self.num_per_page > self._hits:
    7373            return self._hits
    7474        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
    75101
    76102    def _get_hits(self):
    77103        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)
    79108        return self._hits
    80109
    81110    def _get_pages(self):
  • django/views/generic/list_detail.py

     
    55from django.core.exceptions import ObjectDoesNotExist
    66
    77def 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',
    1011        mimetype=None):
    1112    """
    1213    Generic list of objects.
     
    3334            number of pages, total
    3435        hits
    3536            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)
    3650    """
    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
    3955    if paginate_by:
    4056        paginator = ObjectPaginator(queryset, paginate_by)
    4157        if not page:
     
    5975            'previous': page - 1,
    6076            'pages': paginator.pages,
    6177            '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),
    6281        }, context_processors)
    6382    else:
    6483        c = RequestContext(request, {
     
    6786        }, context_processors)
    6887        if not allow_empty and len(queryset) == 0:
    6988            raise Http404
     89           
    7090    for key, value in extra_context.items():
    7191        if callable(value):
    7292            c[key] = value()
    7393        else:
    7494            c[key] = value
    7595    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())
    78101    t = template_loader.get_template(template_name)
    79102    return HttpResponse(t.render(c), mimetype=mimetype)
    80103
Back to Top