项目场景:
提示:这里简述项目相关背景:
django 3.2自定义404页面,修改了settings.py
与urls.py
,并编写了自定义404函数page_not_found
settings.py
DEBUG = False
ALLOWED_HOSTS = ["*"]
urls.py
from app1 import views
urlpatterns = [
...
]
# 404
handler404 = views.page_not_found
views.py
def page_not_found(request):
return render(request, 'error-404.html')
问题描述
运行时,出现报错:
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:
ERRORS:
?: (urls.E007) The custom handler404 view 'app1.views.page_not_found' does not take the correct number of arguments (request, exception).
System check identified 1 issue (0 silenced).
原因分析:
?: (urls.E007) The custom handler404 view ‘app1.views.page_not_found’ does not take the correct number of arguments (request, exception).
错误提示,自定义handler404
视图未采用正确数量的参数,提示我们参数应该有两个,分别是(request, exception)
检查404函数的写法,缺少exception
参数
解决方案:
修改page_not_found
参数,增加一个exception
def page_not_found(request, exception=None):
return render(request, 'error-404.html')