encode/django-vanilla-views

Handling 404 in ListView with pagination

luanfonceca opened this issue · 4 comments

Hi folks i have an ListView with a pagination and when i access a page out of the index of pages i am receiving a 404 error, can i handle this to return to the first page of something like it?

Thanks in advance.

Hi @luanfonceca.

You'd need to override paginate_queryset in your view.

Once page_number is resolved you'd need to add something like:

page_number = page_number if page_number <= paginator.num_pages else 1

Thanks @carltongibson i will try it later.

o/

No problem.

(depending on your use case I might just override get and catch the Http404... — it might be simpler)

An alternative solution is to set the paginator_class field in your view class to a paginator class you define yourself. You could create your own paginator class by subclassing django.core.paginator.Paginator and then override validate_nubmer() to return a valid number instead of raising an EmptyPage exception.