Django E.408, E.409 and E.410 errors on runserver
Asked Answered
D

6

25

I am installing a new Django project using virtualenv, all in the normal way. My version of Python is 3.7.3, and django is 2.2.3.

If I do python manage.py runserver, I get the following errors:

?: (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.

Here is my settings.py, and you can see that those middleware are there.

"""
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/'

I've tried reinstalling, checking versions, and changing 'MIDDLEWARE_CLASSES' to 'MIDDLEWARE'. Nothing is working. Can anyone identify the problem?

Dopey answered 7/7, 2019 at 15:31 Comment(5)
MIDDLEWARE_CLASSES should 100% be renamed to MIDDLEWARE. Has the correct settings file been set in manage.py?Calyx
Hi Iain, I think the issue is that django-admin should be installing a newer version of startproject. I'm using zsh and googling has suggested that's the issue. But all of my versions are correct...I don't really get it. I'll check manage.pyDopey
Looks like you have multiple versions of Python with different versions on Django installedCalyx
Hi Iain, the fact that MacOS has Python 2.7 installed automatically has been the bane of my (and I'm sure others') life. I've just reinstalled my global django install and it's fixed the issue. Thanks for your help. Hopefully this thread will help others so I'll leave it up.Dopey
No problem, I have run into this issue many times. Maybe your virtualenv was not activated?Calyx
B
37

In Django 2.2.3, activating middlewears in settings is done using the variable MIDDLEWARE not MIDDLEWERE_CLASSES, check the docs here.

So simply change the variable in your settings.py from MIDDLEWARE_CLASSES to MIDDLEWARE.

Most likely this issue occurred due to creating a project with a global django package that had was of version < 2, and then running manage.py runserver with a virtualenv that has local django >= 2

Baleful answered 26/1, 2020 at 15:31 Comment(2)
"most likely ..." - no guessing please, and nothing like that. The reason is much simpler, Django's codebase has been changed. I quoted in my answer, Django's code raising the error.Drop
For me, the "most likely ..." phrase is correct/applicable. I was testing a project created during the days of django version < 2 (actually, version 1.11.3), while I am running django 3.0.4. Renaming MIDDLEWARE_CLASS to MIDDLEWARE solved the problem.Gluten
F
10

Rename variables in settings.py, or add this:

MIDDLEWARE = MIDDLEWARE_CLASSES

to settings.py because new Django triggers errors above based on checks like this:

if not _contains_subclass(
   'django.contrib.auth.middleware.AuthenticationMiddleware', 
   settings.MIDDLEWARE
):
   errors.append(checks.Error( ...
Flay answered 31/7, 2019 at 17:40 Comment(0)
N
7

Change setting.py as pic shows Change 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',
]

Also See: From CSDN

Norrisnorrv answered 26/1, 2020 at 15:20 Comment(0)
S
5

1) Go to settings.py of your project

2) Cut these three lines from MIDDLEWARE_CLASSES :

    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware',

3) Now paste the code given below right after MIDDLEWARE_CLASSES :

MIDDLEWARE = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
]

Will look like this: enter image description here

Sleepyhead answered 17/4, 2020 at 14:26 Comment(0)
C
0

change like it and keep the both ke MIDDLEWARE_CLASSES and MIDDLEWARE classes

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

MIDDLEWARE = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',  
]
Credible answered 28/10, 2021 at 7:45 Comment(0)
R
0

If you have a custom middleware, try to import it in manage.py shell. Sometimes Django can blame other middlewares when it cannot import yours (because of not installed dependency for example).

Remunerative answered 11/2, 2022 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.