回到顶部

Contents

django2 The custom handler404 view 'users.views.page_not_found' does not take the correct number of arguments (request, exception).

错误日志

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)

 


^_^
请喝咖啡 ×

前一篇: Django CKEditor 上传图片提示“不正确的服务器响应”的解决办法
下一篇: HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
captcha