django2 The custom handler404 view 'users.views.page_not_found' does not take the correct number of
2023-03-07 09:47:26 时间
错误日志
django 2.2 配置全局 404、500 时报错:
The custom handler404 view 'users.views.page_not_found' does not take the correct number of arguments (request, exception)
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "d:\program files\python36\Lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "d:\program files\python36\Lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "E:\py_virtualenv\qingjiu\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "E:\py_virtualenv\qingjiu\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "E:\py_virtualenv\qingjiu\lib\site-packages\django\core\management\base.py", line 436, in check
raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:
ERRORS:
?: (urls.E007) The custom handler404 view 'users.views.page_not_found' does not take the correct number of arguments (request, exception).
System check identified 1 issue (0 silenced).
错误代码
django 1.11 下不会报错
# views.py
from django.shortcuts import render, render_to_response, HttpResponseRedirect
def page_not_found(request):
"""
404 页面
:param request:
:return:
"""
return render_to_response("404.html")
def server_error(request):
"""
500 页面
:param request:
:return:
"""
return render_to_response("500.html")
# urls.py
# 自定义错误页面,不用导入 handler400
# http://doc.codingdict.com/django/topics/http/views.html#customizing-error-views
handler404 = page_not_found
handler500 = server_error
解决方案
render 加一个 status=404,500 需再加 exception 参数
def page_not_found(request, exception=None):
"""
404 页面
:param request:
:return:
"""
return render(request, "404.html", status=404)
def server_error(request, exception=None):
"""
500 页面
:param request:
:return:
"""
return render(request, "500.html", status=500)
解决思路
https://docs.djangoproject.com/en/2.2/topics/http/views/
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.test import SimpleTestCase, override_settings
from django.urls import path
def response_error_handler(request, exception=None):
return HttpResponse('Error handler content', status=403)
def permission_denied_view(request):
raise PermissionDenied
urlpatterns = [
path('403/', permission_denied_view),
]
handler403 = response_error_handler
# ROOT_URLCONF must specify the module that contains handler403 = ...
@override_settings(ROOT_URLCONF=__name__)
class CustomErrorHandlerTests(SimpleTestCase):
def test_handler_renders_template_response(self):
response = self.client.get('/403/')
# Make assertions on the response here. For example:
self.assertContains(response, 'Error handler content', status_code=403)
相关文章
- 在 Go 里用 CGO?这 7 个问题你要关注!
- 9款优秀的去中心化通讯软件 Matrix 的客户端
- 求职数据分析,项目经验该怎么写
- 在OKR中,我看到了数据驱动业务的未来
- 火山引擎云原生大数据在金融行业的实践
- OpenHarmony富设备移植指南(二)—从postmarketOS获取移植资源
- 《数据成熟度指数》报告:64%的企业领袖认为大多数员工“不懂数据”
- OpenHarmony 小型系统兼容性测试指南
- 肯睿中国(Cloudera):2023年企业数字战略三大趋势预测
- 适用于 Linux 的十大命令行游戏
- GNOME 截图工具的新旧截图方式
- System76 即将推出的 COSMIC 桌面正在酝酿大变化
- 2GB 内存 8GB 存储即可流畅运行,Windows 11 极致精简版系统 Tiny11 发布
- 迎接 ecode:一个即将推出的具有全新图形用户界面框架的现代、轻量级代码编辑器
- loongarch架构介绍(三)—地址翻译
- Go 语言怎么解决编译器错误“err is shadowed during return”?
- 敏捷:可能被开发人员遗忘的部分
- Denodo预测2023年数据管理和分析的未来
- 利用数据推动可持续发展
- 在 Vue3 中实现 React 原生 Hooks(useState、useEffect),深入理解 React Hooks 的