1

I am trying to pass variables to the 404.html template. I am able to call 404.html but not able to pass any values. How can I do this?

I'd like to have something like this.

raise Http404({'error', 'acb_error')
user2307087
  • 423
  • 11
  • 25

1 Answers1

0

Check out The Http404 Exception docs:

For convenience, and because it’s a good idea to have a consistent 404 error page across your site, Django provides an Http404 exception. If you raise Http404 at any point in a view function, Django will catch it and return the standard error page for your application, along with an HTTP error code 404.

Here is the example usage from the docs:

from django.http import Http404
from django.shortcuts import render_to_response
from polls.models import Poll

def detail(request, poll_id):
    try:
        p = Poll.objects.get(pk=poll_id)
    except Poll.DoesNotExist:
        raise Http404("Poll does not exist")
    return render_to_response('polls/detail.html', {'poll': p})

Edit:

Realized that this won't work for you unless you have DEBUG = True; therefore, you can use something like:

return HttpResponseNotFound('<h1>Error</h1><p>acb_error</p>')

Also, check out the answers here for tips on passing custom data to a standard 404 page.

Community
  • 1
  • 1
Mike Covington
  • 2,147
  • 1
  • 16
  • 26