我正在使用自定义mixin将过滤器应用于使用MultipleObjectMixin分页的基于类的视图的查询集。当我应用过滤器使得当前页面在数据集范围之外时,我显然得到404.我想要做的是捕获此异常并将其重定向到新的最后一页。
我对相关视图的get_queryset:
def get_queryset(self):
filters = self.build_location_filter()
if not filters == None:
return models.Account.corporateAccounts(locations=self.build_location_filter)
else:
return models.Account.corporateAccounts()
有什么建议吗?
答案 0 :(得分:4)
尝试:
def get(self, request, *args, **kwargs):
try:
return super(MyView, self).get(request, *args, **kwargs)
except Http404:
if kwargs['page'] > self.paginator.num_pages:
return HttpResponseRedirect(reverse('this_view_paged', kwargs={'page': self.paginator.num_pages}))
else:
# re-raise Http404, as the reason for the 404 was not that maximum pages was exceeded
raise Http404(_(u"Empty list and '%(class_name)s.allow_empty' is False.")
% {'class_name': self.__class__.__name__})
<强>更新强>
对不起。我真的认为Django将paginator
添加为实例变量。 真的糟透了它没有,因为它使它比它需要的难度高100倍。分页器在paginate_queryset
中被捕获,这实际上是Http404
被提升的地方。这意味着,您甚至无法调用此方法来获取您的分页器,因为您从中获得的所有内容都是例外。所以,你必须更深入,不幸的是重复一些Django视图逻辑,我讨厌做,但这是我能看到的唯一前进道路。由于我们正在使用代码重复,无论如何,我现在正在覆盖整个paginate_queryset
方法。新的代码直接从Django源代码中复制,并在注释部分中进行了修改(我已将上面的原始代码完整保留给后代):
def paginate_queryset(self, queryset, page_size):
"""
Paginate the queryset, if needed.
"""
paginator = self.get_paginator(queryset, page_size, allow_empty_first_page=self.get_allow_empty())
page = self.kwargs.get('page') or self.request.GET.get('page') or 1
try:
page_number = int(page)
except ValueError:
if page == 'last':
page_number = paginator.num_pages
else:
raise Http404(_(u"Page is not 'last', nor can it be converted to an int."))
try:
page = paginator.page(page_number)
# Moving this line after the try/except block because DRY
#return (paginator, page, page.object_list, page.has_other_pages())
except InvalidPage:
# This used to raise a 404, but we're replacing this functionality
#raise Http404(_(u'Invalid page (%(page_number)s)') % {
# 'page_number': page_number
#})
page = paginator.page(paginator.num_pages)
return (paginator, page, page.object_list, page.has_other_pages())