def list(self, request, queryset, page=None): """ Renders a list of model objects to HttpResponse. """ template_name = '%s/%s_list.html' % (self.template_dir, queryset.model._meta.module_name) if self.paginate_by: paginator = ObjectPaginator(queryset, self.paginate_by) if not page: page = request.GET.get('page', 1) try: page = int(page) object_list = paginator.get_page(page - 1) except (InvalidPage, ValueError): if page == 1 and self.allow_empty: object_list = [] else: raise Http404 c = RequestContext( request, { '%s_list' % self.template_object_name: object_list, 'is_paginated': paginator.pages > 1, 'results_per_page': self.paginate_by, 'has_next': paginator.has_next_page(page - 1), 'has_previous': paginator.has_previous_page(page - 1), 'page': page, 'next': page + 1, 'previous': page - 1, 'last_on_page': paginator.last_on_page(page - 1), 'first_on_page': paginator.first_on_page(page - 1), 'pages': paginator.pages, 'hits': paginator.hits, }, self.context_processors) else: object_list = queryset c = RequestContext( request, { '%s_list' % self.template_object_name: object_list, 'is_paginated': False }, self.context_processors) if not self.allow_empty and len(queryset) == 0: raise Http404 # Hide unexposed fields for obj in object_list: self._hide_unexposed_fields(obj, self.expose_fields) c.update(self.extra_context) t = self.template_loader.get_template(template_name) return HttpResponse(t.render(c), mimetype=self.mimetype)
def list(self, request, queryset, page=None): """ Renders a list of model objects to HttpResponse. """ template_name = '%s/%s_list.html' % (self.template_dir, queryset.model._meta.module_name) if self.paginate_by: paginator = ObjectPaginator(queryset, self.paginate_by) if not page: page = request.GET.get('page', 1) try: page = int(page) object_list = paginator.get_page(page - 1) except (InvalidPage, ValueError): if page == 1 and self.allow_empty: object_list = [] else: raise Http404 c = RequestContext(request, { '%s_list' % self.template_object_name: object_list, 'is_paginated': paginator.pages > 1, 'results_per_page': self.paginate_by, 'has_next': paginator.has_next_page(page - 1), 'has_previous': paginator.has_previous_page(page - 1), 'page': page, 'next': page + 1, 'previous': page - 1, 'last_on_page': paginator.last_on_page(page - 1), 'first_on_page': paginator.first_on_page(page - 1), 'pages': paginator.pages, 'hits' : paginator.hits, }, self.context_processors) else: object_list = queryset c = RequestContext(request, { '%s_list' % self.template_object_name: object_list, 'is_paginated': False }, self.context_processors) if not self.allow_empty and len(queryset) == 0: raise Http404 # Hide unexposed fields for obj in object_list: self._hide_unexposed_fields(obj, self.expose_fields) c.update(self.extra_context) t = self.template_loader.get_template(template_name) return HttpResponse(t.render(c), mimetype=self.mimetype)
def object_list(request, queryset, paginate_by=None, page=None, allow_empty=True, template_name=None, template_loader=loader, extra_context=None, context_processors=None, template_object_name='object', mimetype=None): """ Generic list of objects. Templates: ``<app_label>/<model_name>_list.html`` Context: object_list list of objects is_paginated are the results paginated? results_per_page number of objects per page (if paginated) has_next is there a next page? has_previous is there a prev page? page the current page next the next page previous the previous page pages number of pages, total hits number of objects, total last_on_page the result number of the last of object in the object_list (1-indexed) first_on_page the result number of the first object in the object_list (1-indexed) page_range: A list of the page numbers (1-indexed). """ if extra_context is None: extra_context = {} queryset = queryset._clone() if paginate_by: paginator = ObjectPaginator(queryset, paginate_by) if not page: page = request.GET.get('page', 1) try: page_number = int(page) except ValueError: if page == 'last': page_number = paginator.pages else: # Page is not 'last', nor can it be converted to an int raise Http404 try: object_list = paginator.get_page(page_number - 1) except InvalidPage: if page_number == 1 and allow_empty: object_list = [] else: raise Http404 c = RequestContext(request, { '%s_list' % template_object_name: object_list, 'is_paginated': paginator.pages > 1, 'results_per_page': paginate_by, 'has_next': paginator.has_next_page(page_number - 1), 'has_previous': paginator.has_previous_page(page_number - 1), 'page': page_number, 'next': page_number + 1, 'previous': page_number - 1, 'last_on_page': paginator.last_on_page(page_number - 1), 'first_on_page': paginator.first_on_page(page_number - 1), 'pages': paginator.pages, 'hits' : paginator.hits, 'page_range' : paginator.page_range }, context_processors) else: c = RequestContext(request, { '%s_list' % template_object_name: queryset, 'is_paginated': False }, context_processors) if not allow_empty and len(queryset) == 0: raise Http404 for key, value in extra_context.items(): if callable(value): c[key] = value() else: c[key] = value if not template_name: model = queryset.model template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower()) t = template_loader.get_template(template_name) return HttpResponse(t.render(c), mimetype=mimetype)
class Paginator(object): """ Much like Django's ``ObjectPaginator`` (which it uses), but always represents a specific current page instead of just providing an interface to the data. Based on the active page it makes a number of properties available which mostly work exactly like the context that the ``object_list`` generic view provides. ``__init__`` takes the same arguments as ``ObjectPaginator``, plus an additional parameter ``page`` to initialize the active page number. It does not need to be an int, i.e. it can come directly from ``request.GET``; if conversion to an integer fails, a ``Http404`` exception is raised. You can also later assign to to ``page`` attribute. >>> items = range(1,6789) >>> paginator = Paginator(items, num_per_page=10, page=3) >>> paginator.items [21, 22, 23, 24, 25, 26, 27, 28, 29, 30] >>> paginator.is_paginated True >>> paginator.results_per_page 10 >>> paginator.has_next True >>> paginator.has_previous True >>> paginator.page 3 >>> paginator.page0 2 >>> paginator.next 4 >>> paginator.previous 2 >>> paginator.last_on_page 30 >>> paginator.first_on_page 21 >>> paginator.pages 679 >>> paginator.hits 6788 """ def __init__(self, *args, **kwargs): page = kwargs.pop('page', 1) self._paginator = DjangoPaginator(*args, **kwargs) # Resolve the page number; this is similar to what the Django # ``object_list`` generic view is doing. try: self.page = int(page) except ValueError: if page == 'last': self.page = self._paginator.pages else: raise Http404 def _set_page(self, new_page): self.__page = new_page try: self.object_list = self._paginator.get_page(new_page-1) except InvalidPage: if new_page != -1: raise Http404 else: self.object_list = [] # allow empty self.items = self.object_list # alias # update all exposed info for the new page number self.update_attrs() def _get_page(self): return self.__page page = property(_get_page, _set_page) def update_attrs(self): """Override in descendants to set custom fields.""" self.page0 = self.page-1 self.is_paginated = self._paginator.pages > 1 self.results_per_page = self._paginator.num_per_page self.has_next = self._paginator.has_next_page(self.page-1) self.has_previous = self._paginator.has_previous_page(self.page-1) self.next = self.page+1 self.previous = self.page-1 self.last_on_page = self._paginator.last_on_page(self.page-1) self.first_on_page = self._paginator.first_on_page(self.page-1) self.pages = self._paginator.pages self.hits = self._paginator.hits self.page_range = self._paginator.page_range
def object_list(request, queryset, paginate_by=None, page=None, allow_empty=False, template_name=None, template_loader=loader, extra_context=None, context_processors=None, template_object_name='object', mimetype=None): """ Generic list of objects. Templates: ``<app_label>/<model_name>_list.html`` Context: object_list list of objects is_paginated are the results paginated? results_per_page number of objects per page (if paginated) has_next is there a next page? has_previous is there a prev page? page the current page next the next page previous the previous page pages number of pages, total hits number of objects, total last_on_page the result number of the last of object in the object_list (1-indexed) first_on_page the result number of the first object in the object_list (1-indexed) """ if extra_context is None: extra_context = {} queryset = queryset._clone() if paginate_by: paginator = ObjectPaginator(queryset, paginate_by) if not page: page = request.GET.get('page', 1) try: page = int(page) object_list = paginator.get_page(page - 1) except (InvalidPage, ValueError): if page == 1 and allow_empty: object_list = [] else: raise Http404 c = RequestContext( request, { '%s_list' % template_object_name: object_list, 'is_paginated': paginator.pages > 1, 'results_per_page': paginate_by, 'has_next': paginator.has_next_page(page - 1), 'has_previous': paginator.has_previous_page(page - 1), 'page': page, 'next': page + 1, 'previous': page - 1, 'last_on_page': paginator.last_on_page(page - 1), 'first_on_page': paginator.first_on_page(page - 1), 'pages': paginator.pages, 'hits': paginator.hits, }, context_processors) else: c = RequestContext(request, { '%s_list' % template_object_name: queryset, 'is_paginated': False }, context_processors) if not allow_empty and len(queryset) == 0: raise Http404 for key, value in extra_context.items(): if callable(value): c[key] = value() else: c[key] = value if not template_name: model = queryset.model template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower()) t = template_loader.get_template(template_name) return HttpResponse(t.render(c), mimetype=mimetype)