Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try:</p> <pre><code>def get(self, request, *args, **kwargs): try: return super(MyView, self).get(request, *args, **kwargs) except Http404: if kwargs['page'] &gt; 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__}) </code></pre> <p><strong>UPDATE</strong></p> <p>Sorry. I really thought that Django added <code>paginator</code> as an instance variable. It <em>really</em> sucks that it doesn't, because it makes this 100 times more difficult than it needs to be. The paginator is squirreled away in <code>paginate_queryset</code>, which is actually where the <code>Http404</code> gets raised from. That means, you can't even call this method to get your paginator, as all you'll ever get from it is an exception. So, you have to go deeper, unfortunately duplicating some of the Django view logic, which I <em>hate</em> to do, but it's the only path forward I can see. Since we're resorting to code duplication, regardless, I'm now overriding the entire <code>paginate_queryset</code> method. The new code is copied straight from the Django source, with modifications noted in commented sections (I've left the original code above intact for posterity):</p> <pre><code>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()) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload