django2 The custom handler404 view 'users.views.page_not_found' does not take the correct

2023-02-18 14:20:22 浏览数 (1)

错误日志

django 2.2 配置全局 404、500 时报错:

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

代码语言:javascript复制
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "d:program filespython36Libthreading.py", line 916, in _bootstrap_inner
    self.run()
  File "d:program filespython36Libthreading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "E:py_virtualenvqingjiulibsite-packagesdjangoutilsautoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "E:py_virtualenvqingjiulibsite-packagesdjangocoremanagementcommandsrunserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "E:py_virtualenvqingjiulibsite-packagesdjangocoremanagementbase.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 下不会报错

代码语言:javascript复制
# 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 参数

代码语言:javascript复制
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/

代码语言:javascript复制
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)

0 人点赞