Display a specific template when Django raises an exception

1.3k Views Asked by At

In my django app, I have multiple places where I raise a specific custom exception DeserializationError. My goal is to show/redirect to a pretty page to show the user this error, including the error message, when this error is raised. Basically, a page that says something like

Something went wrong. Please contact [email protected].
Error: DeserializationError. Message: SomeModel, "somemodel", does not exist.

Would this even be possible? I've been trying to search for a solution but haven't been able to find anything yet.

2

There are 2 best solutions below

2
On BEST ANSWER

Most likely such errors will return HTTP 500 server error.

In django you can write your own custom view to handle such cases and return your own page with the html that you like.

The 500 (server error) view explains writing server error views. There are more types of errors handled as explained on same page.

0
On

An option for handling HTTP 500 errors, add this to your Settings file,

handler500 = 'mysite.views.my_custom_error_view'

and in the view, you can render the "error page" using

HttpResponseNotFound('<h1>Page not found</h1>')

the server_error() view will be overridden by handler500.