Custom Django 404 error

前端 未结 3 1363
时光取名叫无心
时光取名叫无心 2020-12-31 14:04

I have a 404.html page, but in some cases I want to be able to send a json error message (for 404 and 500, etc.). I read the following page:

https://docs.djangoproje

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-31 14:51

    Basics:

    To define custom view for handling 404 errors, define in the URL config, a view for handler404, like handler404 = 'views.error404'

    Apart from the basics, some things to note about (custom 404 views):

    1. It will be enabled only in Debug=False mode.
    2. And more ignored one, across most answers (and this this stuck my brains out).

      The 404 view defaults to

      django.views.defaults.page_not_found(request, exception, template_name='404.html')

      Notice the parameter exception

      This was causing a 404 to 500 redirect from within def get_exception_response(self, request, resolver, status_code, exception) function defined in core.handlers.base since it could not find the parameter exception

提交回复
热议问题