How to redefine a 404 error to send arguments to a function?

It is necessary to poison the argument error and text

 # views.py def handler404(request, error, text): response = render_to_response('error/40X.html', {'error': error, 'text':text}) response.status_code = error return response 

And the 404 error override code:

 handler404 = 'app.views.handler404' 

I use Django v1.10 and Python v3.5, and I don’t really want to create a function for each error.

  • Where will text and error be transmitted? - Sardorbek Imomaliev
  • @SardorbekImomaliev needs to be transferred from mysite / urls.py where handler404 = 'app.views.handler404' , perhaps there is another way to set a unique page for HTTP errors - users

1 answer 1

I don't think you need to use handler404 . Django page_not_found does what you want.

In Django 1.9+, you can include {{ exception }} in the 404 template.

In your opinion, it would be possible to set a message when raising an exception, for example:

 from django.http import Http404 def my_view(request): raise Http404('custom error message') 

It makes no sense to add response.status_code to handler404 if the response code is always 404.

Translation answer from stackoverflow.com from @alasdair