2π
The methodology here involves a couple things. First, within those views, you want to make sure you are getting those errors within an except
block as your code does. Then you should return an HttpResponseRedirect
object redirecting the user to the error page or another page where you would like for the user to return.
The next thing you can do is return a particular view for all errors of a type on the site when they hit urls.py
. For example, to redirect all 404 errors, add the following code to the base site urls.py
handler404 = 'App.views.error_404_view'
Then, in the view simply return the custom error page youβd like or perform a new HttpResponseRedirect
def error_404_view(request, exception):
return render(request, 'error_404.html')
2π
First of all you should think on what errors you want to expose. Usually 4xx errors (Errors that are attributed to the client-side) are disclosed so the user may correct the request. On the other side, 5xx errors (Errors that are attributed to the server-side) are usually only presented without information.
try something like that β
try:
test_method(form.cleaned_data)
except `PermissionError` as e:
status_code= 403
- [Django]-Django: Values_list returns id from choice field instead of name
- [Django]-Search for item in list with 2MILLION items β Python
- [Django]-Django serving each app separately in each its port
- [Django]-Access to fields of prefetch_related on reverse relation
- [Django]-Property decorator eager load and eager load in general using Django