encode / django-vanilla-views

Beautifully simple class-based views.
http://django-vanilla-views.org/
BSD 2-Clause "Simplified" License
985 stars 74 forks source link

Handling 404 in ListView with pagination #49

Closed luanfonceca closed 8 years ago

luanfonceca commented 8 years ago

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.

carltongibson commented 8 years ago

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
luanfonceca commented 8 years ago

Thanks @carltongibson i will try it later.

o/

carltongibson commented 8 years ago

No problem.

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

gogognome commented 5 years ago

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.