当我运行服务器时python3 manage.py runserver
浏览器返回A server error occurred. Please contact the administrator.
然后我得到这个错误

`Traceback (most recent call last):
  File "/usr/lib/python3.5/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/wsgi.py", line 170, in __call__
    self.load_middleware()
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 52, in load_middleware
    mw_instance = mw_class()
TypeError: __init__() missing 1 required positional argument: 'get_response'

我的设置文件是那样的
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts',
]

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'web.middleware.LoginRequiredMiddleware',
]

在五月的项目中我创建了middleware.py
from django.conf import settings

class LoginRequiredMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        return response

那你有什么想法?!

最佳答案

您的中间件是一种新型的中间件,因此您应该在设置中使用MIDDLEWARE而不是MIDDLEWARE_CLASSES

关于python - TypeError:__init __()缺少1个必需的位置参数:“get_response”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46354535/

10-09 21:11