runserver 上的 Django E.408、E.409 和 E.410 错误

Rap*_*117 18 python django admin

我正在使用 virtualenv 以正常方式安装一个新的 Django 项目。我的Python版本是3.7.3,django是2.2.3。

如果我执行 python manage.py runserver,我会收到以下错误:

?: (admin.E408)'django.contrib.auth.middleware.AuthenticationMiddleware' must be in MIDDLEWARE in order to use the admin application.
?: (admin.E409) 'django.contrib.messages.middleware.MessageMiddleware' must be in MIDDLEWARE in order to use the admin application.
?: (admin.E410) 'django.contrib.sessions.middleware.SessionMiddleware' must be in MIDDLEWARE in order to use the admin application.
Run Code Online (Sandbox Code Playgroud)

这是我的settings.py,你可以看到那些中间件在那里。

"""
Django settings for fcc_new project.

Generated by 'django-admin startproject' using Django 1.9.4.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = MY KEY goes here

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

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',
]

ROOT_URLCONF = 'fcc_new.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'fcc_new.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'
Run Code Online (Sandbox Code Playgroud)

我尝试重新安装、检查版本并将“MIDDLEWARE_CLASSES”更改为“MIDDLEWARE”。没有任何工作。任何人都可以找出问题吗?

Nad*_*xan 29

在 Django 2.2.3 中,在设置中激活中间件是使用变量MIDDLEWAREnot完成的MIDDLEWERE_CLASSES,请在此处查看文档。

因此,只需更改settings.pyfrom 中的变量MIDDLEWARE_CLASSES即可MIDDLEWARE

出现此问题的原因很可能是创建一个具有django版本 < 2的全局包的项目,然后manage.py runserver使用具有 local django>= 2的 virtualenv运行

  • 对我来说,“最有可能......”这句话是正确/适用的。我正在测试一个在 `django` 版本​​ &lt; 2(实际上是版本 1.11.3)期间创建的项目,同时我正在运行 `django` 3.0.4。将“MIDDLEWARE_CLASS”重命名为“MIDDLEWARE”解决了问题。 (3认同)

Sła*_*art 9

在 中重命名变量settings.py,或添加以下内容:

MIDDLEWARE = MIDDLEWARE_CLASSES
Run Code Online (Sandbox Code Playgroud)

settings.py 因为新的Django触发基于这样的支票上面的错误:

if not _contains_subclass(
   'django.contrib.auth.middleware.AuthenticationMiddleware', 
   settings.MIDDLEWARE
):
   errors.append(checks.Error( ...
Run Code Online (Sandbox Code Playgroud)


小智 6

如图所示 更改 setting.py 更改 setting.py

# MIDDLEWARE_CLASSES = [
MIDDLEWARE = [
    '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',
]
Run Code Online (Sandbox Code Playgroud)

另见:来自CSDN


小智 5

1)转到项目的settings.py

2)从 MIDDLEWARE_CLASSES 中删除这三行:

    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware',
Run Code Online (Sandbox Code Playgroud)

3)现在将下面给出的代码粘贴到 MIDDLEWARE_CLASSES 之后:

MIDDLEWARE = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
]
Run Code Online (Sandbox Code Playgroud)

看起来像这样: 在此输入图像描述