The Django docs show how to return the last page of a paginated queryset using a function-based view by catching the EmptyPage
exception.
What's the easiest way to achieve the same thing using generic class-based views, for example ListView
?
I first thought that the allow_empty
setting for MultipleObjectMixin
would do what I need, but examining the code shows that it only prevents a 404 error if there are zero objects in the queryset, rather than zero objects on the page requested.
Two options seem to be:
- subclass
ListView
and overridepaginate_queryset
(inherited fromMultipleObjectMixin
), or - subclass
Paginator
and overridevalidate_number
, and setpaginator_class
to the subclass in the view.
Is there a better way to achieve this?