24

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?

Raph117
  • 3,441
  • 7
  • 29
  • 50
  • 4
    `MIDDLEWARE_CLASSES` should 100% be renamed to `MIDDLEWARE`. Has the correct settings file been set in manage.py? – Iain Shelvington Jul 07 '19 at 15:40
  • 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.py – Raph117 Jul 07 '19 at 15:41
  • Looks like you have multiple versions of Python with different versions on Django installed – Iain Shelvington Jul 07 '19 at 15:44
  • 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. – Raph117 Jul 07 '19 at 15:47
  • No problem, I have run into this issue many times. Maybe your virtualenv was not activated? – Iain Shelvington Jul 07 '19 at 15:51

6 Answers6

35

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

Nader Alexan
  • 2,127
  • 22
  • 36
  • "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. – Sławomir Lenart Feb 18 '20 at 16:41
  • 3
    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. – Gathide Apr 13 '20 at 11:29
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( ...
Sławomir Lenart
  • 7,543
  • 4
  • 45
  • 61
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

cobra8626
  • 71
  • 1
  • 2
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

aminography
  • 21,986
  • 13
  • 70
  • 74
Faizun Faria
  • 151
  • 1
  • 6
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',  
]
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
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).

lampslave
  • 1,431
  • 1
  • 15
  • 20