Override get_paginator
in your class and run your checks there. Then override dispatch
to redirect or return the normal view:
class MyListView(ListView):
...
def get_paginator(self, queryset, per_page, orphans=0, allow_empty_first_page=True):
paginator = super(MyListView, self).get_paginator(queryset, per_page, orphans=orphans, allow_empty_first_page=allow_empty_first_page)
try:
paginator.page(self.kwargs.get('page', 1))
except EmptyPage: # or InvalidPage, but that's less precise
self.is_empty = True
return paginator
def dispatch(self, request, *args, **kwargs):
response = super(MyListView, self).dispatch(request, *args, **kwargs)
if getattr(self, 'is_empty', False):
return HttpResponseRedirect('/some/other/url/')
else:
return response