Django 自定义 404 处理程序

zub*_*hav 0 python django

我们最近升级到 Django 1.10,我们在实时站点上收到此错误:

内置:TypeError custom_404() 得到了一个意外的关键字参数“异常”

代码如下:

网址.py

urlpatterns = [ ... ]
handler404 = global_views.custom_404
handler500 = global_views.custom_500
Run Code Online (Sandbox Code Playgroud)

global_views.py

def custom_404(request, exception, template_name='404.html'):
    return page_not_found(request, exception, template_name=template_name)   

def custom_500(request, template_name='500.html'):
    return server_error(request, template_name=template_name) 
Run Code Online (Sandbox Code Playgroud)

我们已经尝试了该视图的许多变体,但都导致了该错误。

怎么了?

po5*_*o5i 6

您的函数应定义为:

def custom_404(request, exception=None, template_name='404.html'):

花我几个小时来弄清楚这一点。