mirror of https://github.com/django/django.git
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
This commit is contained in:
parent
d67208f5be
commit
08d4910d69
|
@ -1,7 +1,7 @@
|
||||||
from django.template import loader, RequestContext
|
from django.template import loader, RequestContext
|
||||||
from django.http import Http404, HttpResponse
|
from django.http import Http404, HttpResponse
|
||||||
from django.core.xheaders import populate_xheaders
|
from django.core.xheaders import populate_xheaders
|
||||||
from django.core.paginator import ObjectPaginator, InvalidPage
|
from django.core.paginator import QuerySetPaginator, InvalidPage
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
|
|
||||||
def object_list(request, queryset, paginate_by=None, page=None,
|
def object_list(request, queryset, paginate_by=None, page=None,
|
||||||
|
@ -45,43 +45,47 @@ def object_list(request, queryset, paginate_by=None, page=None,
|
||||||
if extra_context is None: extra_context = {}
|
if extra_context is None: extra_context = {}
|
||||||
queryset = queryset._clone()
|
queryset = queryset._clone()
|
||||||
if paginate_by:
|
if paginate_by:
|
||||||
paginator = ObjectPaginator(queryset, paginate_by)
|
paginator = QuerySetPaginator(queryset, paginate_by, allow_empty_first_page=allow_empty)
|
||||||
if not page:
|
if not page:
|
||||||
page = request.GET.get('page', 1)
|
page = request.GET.get('page', 1)
|
||||||
try:
|
try:
|
||||||
page_number = int(page)
|
page_number = int(page)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
if page == 'last':
|
if page == 'last':
|
||||||
page_number = paginator.pages
|
page_number = paginator.num_pages
|
||||||
else:
|
else:
|
||||||
# Page is not 'last', nor can it be converted to an int
|
# Page is not 'last', nor can it be converted to an int.
|
||||||
raise Http404
|
raise Http404
|
||||||
try:
|
try:
|
||||||
object_list = paginator.get_page(page_number - 1)
|
page_obj = paginator.page(page_number)
|
||||||
except InvalidPage:
|
except InvalidPage:
|
||||||
if page_number == 1 and allow_empty:
|
raise Http404
|
||||||
object_list = []
|
|
||||||
else:
|
|
||||||
raise Http404
|
|
||||||
c = RequestContext(request, {
|
c = RequestContext(request, {
|
||||||
'%s_list' % template_object_name: object_list,
|
'%s_list' % template_object_name: page_obj.object_list,
|
||||||
'is_paginated': paginator.pages > 1,
|
'paginator': paginator,
|
||||||
'results_per_page': paginate_by,
|
'page_obj': page_obj,
|
||||||
'has_next': paginator.has_next_page(page_number - 1),
|
|
||||||
'has_previous': paginator.has_previous_page(page_number - 1),
|
# Legacy template context stuff. New templates should use page_obj
|
||||||
'page': page_number,
|
# to access this instead.
|
||||||
'next': page_number + 1,
|
'is_paginated': page_obj.has_other_pages(),
|
||||||
'previous': page_number - 1,
|
'results_per_page': paginator.per_page,
|
||||||
'last_on_page': paginator.last_on_page(page_number - 1),
|
'has_next': page_obj.has_next(),
|
||||||
'first_on_page': paginator.first_on_page(page_number - 1),
|
'has_previous': page_obj.has_previous(),
|
||||||
'pages': paginator.pages,
|
'page': page_obj.number,
|
||||||
'hits' : paginator.hits,
|
'next': page_obj.next_page_number(),
|
||||||
'page_range' : paginator.page_range
|
'previous': page_obj.previous_page_number(),
|
||||||
|
'last_on_page': page_obj.start_index(),
|
||||||
|
'first_on_page': page_obj.end_index(),
|
||||||
|
'pages': paginator.num_pages,
|
||||||
|
'hits': paginator.count,
|
||||||
|
'page_range': paginator.page_range,
|
||||||
}, context_processors)
|
}, context_processors)
|
||||||
else:
|
else:
|
||||||
c = RequestContext(request, {
|
c = RequestContext(request, {
|
||||||
'%s_list' % template_object_name: queryset,
|
'%s_list' % template_object_name: queryset,
|
||||||
'is_paginated': False
|
'paginator': None,
|
||||||
|
'page_obj': None,
|
||||||
|
'is_paginated': False,
|
||||||
}, context_processors)
|
}, context_processors)
|
||||||
if not allow_empty and len(queryset) == 0:
|
if not allow_empty and len(queryset) == 0:
|
||||||
raise Http404
|
raise Http404
|
||||||
|
|
Loading…
Reference in New Issue