views:

166

answers:

1

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.

+3  A: 

I don't think there's an easy way to display the "this is an error" string in your template. I think that when you raise Http404, the argument is there to aid debugging, rather than display messages on the live website.

If you look at the code for the page_not_found view which is loaded when you raise Http404, you can see that the only variable in the context is

request_path: The path of the requested URL (e.g., '/app/pages/bad_page/')

So in your custom 404.html template file, you can use {{request_path}} to show the requested URL, but the default error handler doesn't provide a way to access the "this is an error" string in the template.

If you really need to add the string to the template context, rather than hardcoding it into your 404 template file, you could write a custom 404 handler.

Alasdair