How to display message sent by Http404 in user 404 error template in Django?

I have a project that I am working on in django. There are many cases where I:

raise Http404("this is an error") 

and for me a good 404 page is created with the error message "this is an error."

Now I want to create my own error page and show it, but I can’t figure out how to do this.

I am sure that this is just a template variable that I need to add to my own 404 template, but I can not find any documentation for it.

+13
django templates
Oct 25 '09 at 15:27
source share
4 answers

As of Django 1.9, the exception is passed to page_not_found , which fires when the Http404 . The error representation 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 , so there was no easy way to include a message from the exception in the template.

One possibility was to use the message structure as @Euribates suggests in his answer. Another was to create a template and return a 404 status code in your opinion instead of raising Http404 .

+8
Oct 25 '09 at 16:31
source

There is another way. The code page_not_found uses RequestContext ; this means that you have access to all the variables defined by all context handlers defined in the TEMPLATE_CONTEXT_PROCESSORS entry in settings.py . The default value includes, among other things, the django message structure.

So, you can 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 opinion as follows:

 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 %} 

This is a bit more complicated, but has the advantage that you can send multiple messages and even use different messages (warning, debugging, information, error, etc.). You can learn more about django's message structure here: Message Structure | Django Documentation .

+6
Apr 18 '13 at 18:24
source

I have a simpler solution

Just write middleware that will accept the error text in some query variable. See Example

 from django.http.response import Http404 class Error404Middleware(object): def process_exception(self, request, exception): if isinstance(exception, Http404): request.error_404_message = exception.message 

At 404.html

 {{ request.error_404_message }} 
+1
Jan 24 '16 at 8:30
source

{{ exception }}

Starting with version 1.9.6, the message is transmitted:

 raise Http404('msg') 

available from templates/404.html as:

 {{ exception }} 

Source: https://github.com/django/django/blob/1.9.6/django/views/defaults.py#L20

+1
May 09 '16 at 7:24
source



All Articles