def render(self, context):
key = self.queryset_var.var
value = self.queryset_var.resolve(context)
if isinstance(self.paginate_by, int):
paginate_by = self.paginate_by
else:
paginate_by = self.paginate_by.resolve(context)
paginator = Paginator(value, paginate_by, self.orphans)
try:
page_obj = paginator.page(context['request'].page)
except InvalidPage:
if INVALID_PAGE_RAISES_404:
raise Http404('Invalid page requested. If DEBUG were set to ' +
'False, an HTTP 404 page would have been shown instead.')
context[key] = []
context['invalid_page'] = True
return ''
if self.context_var is not None:
context[self.context_var] = page_obj.object_list
else:
context[key] = page_obj.object_list
context['paginator'] = paginator
context['page_obj'] = page_obj
return ''
python类InvalidPage()的实例源码
def paginate_queryset(self, queryset, page_size):
"""
Paginate the queryset, if needed.
"""
paginator = self.get_paginator(
queryset, page_size, orphans=self.get_paginate_orphans(),
allow_empty_first_page=self.get_allow_empty())
page_kwarg = self.page_kwarg
page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg) or 1
try:
page_number = int(page)
except ValueError:
if page == 'last':
page_number = paginator.num_pages
else:
raise Http404(_("Page is not 'last', nor can it be converted to an int."))
try:
page = paginator.page(page_number)
return (paginator, page, page.object_list, page.has_other_pages())
except InvalidPage as e:
raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
'page_number': page_number,
'message': str(e)
})
def _get_pagination(objects_list, request):
paginator = Paginator(objects_list, 20)
try:
page = int(request.GET.get('page', '1'))
except:
page = 1
try:
objects = paginator.page(page)
except(EmptyPage, InvalidPage):
objects = paginator.page(1)
# page_range to show at bottom of table
index = objects.number - 1
max_index = len(paginator.page_range)
start_index = index - 4 if index >= 4 else 0
end_index = index + 4 if index <= max_index - 4 else max_index
page_range = paginator.page_range[start_index:end_index]
return objects, page_range
# Status Page
def image_viewer(request: HttpRequest, archive: int, page: int) -> HttpResponse:
images = Image.objects.filter(archive=archive, extracted=True)
if not images:
raise Http404("Archive " + str(archive) + " has no extracted images")
paginator = Paginator(images, 1)
try:
image = paginator.page(page)
except (InvalidPage, EmptyPage):
image = paginator.page(paginator.num_pages)
image_object = image.object_list[0]
if image_object.image_width / image_object.image_height > 1:
image_object.is_horizontal = True
d = {'image': image, 'backurl': redirect(image.object_list[0].archive).url,
'images_range': range(1, images.count() + 1), 'image_object': image_object}
return render(request, "viewer/image_viewer.html", d)
def list_guazi(request):
after_range_num = 5
before_range_num = 4
page_size = 20
Guazi_list = GuaziCar.objects.all().order_by('-id')
paginator = Paginator(Guazi_list, page_size)
try:
page = int(request.GET.get('page','1'))
if page < 1:
page=1
except ValueError:
page=1
try:
carlist = paginator.page(page)
except (EmptyPage,InvalidPage,PageNotAnInteger):
carlist = paginator.page(1)
if page >= after_range_num:
page_range = list(paginator.page_range)[page-after_range_num:page+before_range_num]
else:
page_range = list(paginator.page_range)[0:int(page)+before_range_num]
return render(request, 'guazi.html', {'guazi_list': carlist, 'page_range': page_range})
def get_page(self, data):
try:
page_no = int(self.request.GET.get(self.page_attribute_name, 1))
except (TypeError, ValueError):
raise NotFound('Invalid page number.')
if page_no < 1:
raise NotFound('Page number should be 1 or greater.')
# Explicitly evaluate data before sending it to Paginator, otherwise
# (at least in the case of RelatedSearchQuerySet) the total count
# goes completely wrong
# see: https://github.com/django-haystack/django-haystack/issues/362
data[:self.results_per_page]
paginator = Paginator(data, self.results_per_page)
try:
page = paginator.page(page_no)
except InvalidPage:
raise NotFound('No such page!')
return page
def results(request, template_name="search/results.html"):
""" template for displaying settings.PRODUCTS_PER_PAGE paginated product results """
# get current search phrase
q = request.GET.get('q', '')
# get current page number. Set to 1 is missing or invalid
try:
page = int(request.GET.get('page', 1))
except ValueError:
page = 1
matching = search.products(q).get('products', [])
# generate the pagintor object
paginator = Paginator(matching,
settings.PRODUCTS_PER_PAGE)
try:
results = paginator.page(page).object_list
except (InvalidPage, EmptyPage):
results = paginator.page(1).object_list
search.store(request, q)
page_title = 'Search Results for: ' + q
return render_to_response(template_name, locals(), context_instance=RequestContext(request))
def paginate_queryset(self, queryset, page_size):
"""
Paginate the queryset, if needed.
"""
paginator = self.get_paginator(
queryset, page_size, orphans=self.get_paginate_orphans(),
allow_empty_first_page=self.get_allow_empty())
page_kwarg = self.page_kwarg
page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg) or 1
try:
page_number = int(page)
except ValueError:
if page == 'last':
page_number = paginator.num_pages
else:
raise Http404(_("Page is not 'last', nor can it be converted to an int."))
try:
page = paginator.page(page_number)
return (paginator, page, page.object_list, page.has_other_pages())
except InvalidPage as e:
raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
'page_number': page_number,
'message': str(e)
})
def paginate_dataset(self, dataset, page_size):
paginator = self.get_paginator(
dataset, page_size, orphans=0,
allow_empty_first_page=True)
page_kwarg = self.page_kwarg
page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg)\
or 1
try:
page_number = int(page)
except ValueError:
raise Http404(_('Page cannot be converted to an int.'))
try:
page = paginator.page(page_number)
return (paginator, page, page.object_list, page.has_other_pages())
except InvalidPage as e:
raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
'page_number': page_number,
'message': str(e)
})
def render(self, context):
key = self.queryset_var.var
value = self.queryset_var.resolve(context)
if isinstance(self.paginate_by, int):
paginate_by = self.paginate_by
else:
paginate_by = self.paginate_by.resolve(context)
paginator = Paginator(value, paginate_by, self.orphans)
try:
page_obj = paginator.page(context['request'].page)
except InvalidPage:
if INVALID_PAGE_RAISES_404:
raise Http404('Invalid page requested. If DEBUG were set to ' +
'False, an HTTP 404 page would have been shown instead.')
context[key] = []
context['invalid_page'] = True
return u''
if self.context_var is not None:
context[self.context_var] = page_obj.object_list
else:
context[key] = page_obj.object_list
context['paginator'] = paginator
context['page_obj'] = page_obj
return u''
def getmovielistbystyle(request,page=1):
after_range_num =5
before_range_num=4
try:
page=int(page)
if page<1:
page=1
except ValueError:
page=1
style = request.GET.get('style')
movie_list = Movie.objects.filter(style__contains=u'??')
paginator = Paginator(movie_list,12)
try:
movielist = paginator.page(page)
except(EmptyPage,InvalidPage,PageNotAnInteger):
movielist=paginator.page(1)
if page>=after_range_num:
page_range=paginator.page_range[page-after_range_num:page+before_range_num]
else:
page_range = paginator.page_range[0:int(page)+before_range_num]
return render(request,'movie/allfilms.html',locals())
#??????
def paginate_queryset(self, queryset, page_size):
"""
Paginate the queryset, if needed.
"""
paginator = self.get_paginator(
queryset, page_size, orphans=self.get_paginate_orphans(),
allow_empty_first_page=self.get_allow_empty())
page_kwarg = self.page_kwarg
page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg) or 1
try:
page_number = int(page)
except ValueError:
if page == 'last':
page_number = paginator.num_pages
else:
raise Http404(_("Page is not 'last', nor can it be converted to an int."))
try:
page = paginator.page(page_number)
return (paginator, page, page.object_list, page.has_other_pages())
except InvalidPage as e:
raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
'page_number': page_number,
'message': str(e)
})
def render(self, context):
key = self.queryset_var.var
value = self.queryset_var.resolve(context)
if isinstance(self.paginate_by, int):
paginate_by = self.paginate_by
else:
paginate_by = self.paginate_by.resolve(context)
paginator = Paginator(value, paginate_by, self.orphans)
try:
page_obj = paginator.page(context['request'].page)
except InvalidPage:
if INVALID_PAGE_RAISES_404:
raise Http404('Invalid page requested. If DEBUG were set to ' +
'False, an HTTP 404 page would have been shown instead.')
context[key] = []
context['invalid_page'] = True
return u''
if self.context_var is not None:
context[self.context_var] = page_obj.object_list
else:
context[key] = page_obj.object_list
context['paginator'] = paginator
context['page_obj'] = page_obj
return u''
def route_view(request):
"""
Display a list of global routing table entries which match resources
listed in received certificates.
"""
conf = get_conf(request.user, request.session['handle'])
count = request.GET.get('count', 25)
page = request.GET.get('page', 1)
paginator = Paginator(conf.routes, count)
try:
routes = paginator.page(page)
except InvalidPage:
# page was empty, or page number was invalid
routes = []
ts = dict((attr['name'], attr['ts']) for attr in models.Timestamp.objects.values())
return render(request, 'app/routes_view.html',
{'routes': routes, 'timestamp': ts})
def route_view(request):
"""
Display a list of global routing table entries which match resources
listed in received certificates.
"""
conf = get_conf(request.user, request.session['handle'])
count = request.GET.get('count', 25)
page = request.GET.get('page', 1)
paginator = Paginator(conf.routes, count)
try:
routes = paginator.page(page)
except InvalidPage:
# page was empty, or page number was invalid
routes = []
ts = dict((attr['name'], attr['ts']) for attr in models.Timestamp.objects.values())
return render(request, 'app/routes_view.html',
{'routes': routes, 'timestamp': ts})
def route_view(request):
"""
Display a list of global routing table entries which match resources
listed in received certificates.
"""
conf = get_conf(request.user, request.session['handle'])
count = request.GET.get('count', 25)
page = request.GET.get('page', 1)
paginator = Paginator(conf.routes, count)
try:
routes = paginator.page(page)
except InvalidPage:
# page was empty, or page number was invalid
routes = []
ts = dict((attr['name'], attr['ts']) for attr in models.Timestamp.objects.values())
return render(request, 'app/routes_view.html',
{'routes': routes, 'timestamp': ts})
def route_view(request):
"""
Display a list of global routing table entries which match resources
listed in received certificates.
"""
conf = get_conf(request.user, request.session['handle'])
count = request.GET.get('count', 25)
page = request.GET.get('page', 1)
paginator = Paginator(conf.routes, count)
try:
routes = paginator.page(page)
except InvalidPage:
# page was empty, or page number was invalid
routes = []
ts = dict((attr['name'], attr['ts']) for attr in models.Timestamp.objects.values())
return render(request, 'app/routes_view.html',
{'routes': routes, 'timestamp': ts})
def route_view(request):
"""
Display a list of global routing table entries which match resources
listed in received certificates.
"""
conf = get_conf(request.user, request.session['handle'])
count = request.GET.get('count', 25)
page = request.GET.get('page', 1)
paginator = Paginator(conf.routes, count)
try:
routes = paginator.page(page)
except InvalidPage:
# page was empty, or page number was invalid
routes = []
ts = dict((attr['name'], attr['ts']) for attr in models.Timestamp.objects.values())
return render(request, 'app/routes_view.html',
{'routes': routes, 'timestamp': ts})
def route_view(request):
"""
Display a list of global routing table entries which match resources
listed in received certificates.
"""
conf = get_conf(request.user, request.session['handle'])
count = request.GET.get('count', 25)
page = request.GET.get('page', 1)
paginator = Paginator(conf.routes, count)
try:
routes = paginator.page(page)
except InvalidPage:
# page was empty, or page number was invalid
routes = []
ts = dict((attr['name'], attr['ts']) for attr in models.Timestamp.objects.values())
return render(request, 'app/routes_view.html',
{'routes': routes, 'timestamp': ts})
def paginate_queryset(self, queryset, page_size):
"""
Paginate the queryset, if needed.
"""
paginator = self.get_paginator(
queryset, page_size, orphans=self.get_paginate_orphans(),
allow_empty_first_page=self.get_allow_empty())
page_kwarg = self.page_kwarg
page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg) or 1
try:
page_number = int(page)
except ValueError:
if page == 'last':
page_number = paginator.num_pages
else:
raise Http404(_("Page is not 'last', nor can it be converted to an int."))
try:
page = paginator.page(page_number)
return (paginator, page, page.object_list, page.has_other_pages())
except InvalidPage as e:
raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
'page_number': page_number,
'message': str(e)
})
def indexPaginator(request,id):
""" ????? ?????????? ???????? ????? ?????????
"""
try:
page_num = request.GET['page']
except KeyError:
page_num = 1
cats = Category.objects.all().order_by('name')
if id == None:
cat = Category.objects.first()
else:
cat = Category.objects.get(pk=id)
paginator = Paginator(Good.objects.filter(category=cat).order_by('name'),1)
try:
goods = paginator.page(page_num)
except InvalidPage:
goods = paginator.page(1)
return render(request, "indexPaginator.html", {"category":cat,"cats":cats,"goods":goods})
def blogindex(request):
# need to add pagination etc.
blog_posts = Post.objects.published()
paginator = Paginator(blog_posts, 5)
pagenum = request.GET.get('page', 1)
max_page = paginator.num_pages,
try:
page = paginator.page(pagenum)
except (EmptyPage, InvalidPage):
raise Http404
paginator_html = make_paginator_text('/blogindex/?', int(pagenum), max_page[0])
return render_to_response(
'publicsite_redesign/blogindex.html',
{'post_list': page.object_list,
'paginator_html':paginator_html,
}
)
def blogtag(request, term):
if len(term)<3:
raise Http404
blog_posts = Post.objects.term(term)
paginator = Paginator(blog_posts, 5)
pagenum = request.GET.get('page', 1)
max_page = paginator.num_pages,
try:
page = paginator.page(pagenum)
except (EmptyPage, InvalidPage):
raise Http404
paginator_html = make_paginator_text('/blog/tag/' + term + '/?', int(pagenum), max_page[0])
page_title = "Posts tagged '%s' - page %s of %s" % (term, pagenum, max_page[0])
return render_to_response(
'publicsite_redesign/blogindex.html',
{'post_list': page.object_list,
'paginator_html':paginator_html,
'page_title':page_title,
}
)
def bydate(request,start,end):
docset = Event.objects.daterange(start, end)
paginator = Paginator(docset, 25, orphans=5)
pagenum = request.GET.get('page', 1)
try:
page = paginator.page(pagenum)
except (EmptyPage, InvalidPage):
raise Http404
return render_to_response(
'publicsite/snapshot.html',
{'snapshot_image_name': '',
'page': page, }
)
#
# widgets
#
def paginate_queryset(self, queryset, page_size):
"""
Paginate the queryset, if needed.
"""
paginator = self.get_paginator(
queryset, page_size, orphans=self.get_paginate_orphans(),
allow_empty_first_page=self.get_allow_empty())
page_kwarg = self.page_kwarg
page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg) or 1
try:
page_number = int(page)
except ValueError:
raise Http404(_("Page can not be converted to an int."))
try:
page = paginator.page(page_number)
return (paginator, page, page.object_list, page.has_other_pages())
except InvalidPage as e:
raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
'page_number': page_number,
'message': str(e)
})
def paginate_queryset(self, queryset, page_size):
"""
Paginate the queryset, if needed.
"""
paginator = self.get_paginator(
queryset, page_size, orphans=self.get_paginate_orphans(),
allow_empty_first_page=self.get_allow_empty())
page_kwarg = self.page_kwarg
page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg) or 1
try:
page_number = int(page)
except ValueError:
if page == 'last':
page_number = paginator.num_pages
else:
raise Http404(_("Page is not 'last', nor can it be converted to an int."))
try:
page = paginator.page(page_number)
return (paginator, page, page.object_list, page.has_other_pages())
except InvalidPage as e:
raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
'page_number': page_number,
'message': str(e)
})
def paginate(self):
records = self.report.results
self.paginator = self.report.get_paginator()
result_count = self.paginator.count
self.multi_page = result_count > self.report.get_list_per_page()
self.can_show_all = result_count <= self.report.get_list_max_show_all()
if not (self.show_all and self.can_show_all) and self.multi_page:
try:
records = self.paginator.page(self.page_num + 1).object_list
except InvalidPage:
raise IncorrectLookupParameters
return records
def pages(post_objects, request):
"""
page public function , return page's object tuple
????????????????
"""
paginator = Paginator(post_objects, 20)
try:
current_page = int(request.GET.get('page', '1'))
except ValueError:
current_page = 1
page_range = page_list_return(len(paginator.page_range), current_page)
try:
page_objects = paginator.page(current_page)
except (EmptyPage, InvalidPage):
page_objects = paginator.page(paginator.num_pages)
if current_page >= 5:
show_first = 1
else:
show_first = 0
if current_page <= (len(paginator.page_range) - 3):
show_end = 1
else:
show_end = 0
# ????? ???? ????? ????? ?????????????????????
return post_objects, paginator, page_objects, page_range, current_page, show_first, show_end
def get_results(self, request):
paginator = self.model_admin.get_paginator(request, self.queryset, self.list_per_page)
# Get the number of objects, with admin filters applied.
result_count = paginator.count
# Get the total number of objects, with no admin filters applied.
# Perform a slight optimization:
# full_result_count is equal to paginator.count if no filters
# were applied
if self.model_admin.show_full_result_count:
if self.get_filters_params() or self.params.get(SEARCH_VAR):
full_result_count = self.root_queryset.count()
else:
full_result_count = result_count
else:
full_result_count = None
can_show_all = result_count <= self.list_max_show_all
multi_page = result_count > self.list_per_page
# Get the list of objects to display on this page.
if (self.show_all and can_show_all) or not multi_page:
result_list = self.queryset._clone()
else:
try:
result_list = paginator.page(self.page_num + 1).object_list
except InvalidPage:
raise IncorrectLookupParameters
self.result_count = result_count
self.show_full_result_count = self.model_admin.show_full_result_count
# Admin actions are shown if there is at least one entry
# or if entries are not counted because show_full_result_count is disabled
self.show_admin_actions = not self.show_full_result_count or bool(full_result_count)
self.full_result_count = full_result_count
self.result_list = result_list
self.can_show_all = can_show_all
self.multi_page = multi_page
self.paginator = paginator
def get_results(self, request):
paginator = self.model_admin.get_paginator(request, self.queryset, self.list_per_page)
# Get the number of objects, with admin filters applied.
result_count = paginator.count
# Get the total number of objects, with no admin filters applied.
if self.model_admin.show_full_result_count:
full_result_count = self.root_queryset.count()
else:
full_result_count = None
can_show_all = result_count <= self.list_max_show_all
multi_page = result_count > self.list_per_page
# Get the list of objects to display on this page.
if (self.show_all and can_show_all) or not multi_page:
result_list = self.queryset._clone()
else:
try:
result_list = paginator.page(self.page_num + 1).object_list
except InvalidPage:
raise IncorrectLookupParameters
self.result_count = result_count
self.show_full_result_count = self.model_admin.show_full_result_count
# Admin actions are shown if there is at least one entry
# or if entries are not counted because show_full_result_count is disabled
self.show_admin_actions = not self.show_full_result_count or bool(full_result_count)
self.full_result_count = full_result_count
self.result_list = result_list
self.can_show_all = can_show_all
self.multi_page = multi_page
self.paginator = paginator