I have a project that I am working on in django. There are a lot of instances where I:
raise Http404("this is an error")
and it creates a nice 404 page for me with the error message "this is an error" written on it.
I now want to create a custom error page and have it still display the message, but I can't figure out how.
I'm sure it's just a template variable that I need to add to my custom 404 template, but I can't find any documentation for it.
As of Django 1.9, the exception is passed to the page_not_found
view which runs when you raise Http404
. A representation of the error is passed to the template, you can include it in your template with:
{{ exception }}
In earlier versions, the exception was not passed to the page_not_found
view, so there wasn't an easy way to include the message from the exception in the template.
One possibility was to use the messages framework as @Euribates suggests in their answer. Another was to render a template and return a 404 status code in your view, instead of raising Http404
.