There is another way. The code at page_not_found uses RequestContext; that means that you have access to all the variables defined by all the context processors defined in the TEMPLATE_CONTEXT_PROCESSORS entry in settings.py. The default value includes, among others, the django messages framework.
So, you con define the message you want to show using messages.error, for example, and show the message in the template using the messages variable.
In other words, you can write your view like this:
from django.contrib import messages from django.http import Http404 from django.template import RequestContext def my_view(request): # your code goes here if something_horribly_wrong_happened(): messages.error(request, 'Somethig horribly wrong happened!') raise Http404("It doesn't mind whatever you put here") else: return render_to_response( 'template.html', RequestContext(request, locals()), )
In your 404.html template, you should write something like:
{% if messages %} <ul class="messages"> {% for message in messages %} <li>{{ message }}</li> {% endfor %} </ul> {% endif %}
It's a bit more complex, but it has the advantage you can send several messages, and even use diferent kind of messages (Warning, Debug, Info, Error, etc...) You can read more about the django message framework here: The messages framework | Django Documentation.