Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: Module "django.middleware.common" does not define a "CorsMiddleware" attribute/class #304

Closed
santhoshdc1590 opened this issue Apr 13, 2018 · 7 comments

Comments

@santhoshdc1590
Copy link

Anyone tell me what is happening here? How do I correct this?

Python 3.5

pip installed

django==1.11.1
djangorestframework==3.8.2
django-cors-headers==2.2.0

This is my settings.py


"""
Django settings for mysite project.

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

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

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

import os
from corsheaders.defaults import default_methods
from corsheaders.defaults import default_headers
# 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.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '1+fkbb#o$j3&2xc=90k(cn^^41$^=5$w9#3-3frm2y4cz$%2qy'

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

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'mysite.myapp',
    'corsheaders',
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CorsMiddleware',
]

ROOT_URLCONF = 'mysite.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',
            ],
        },
    },
]

MIDDLEWARE_CLASSES = [
   
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'corsheaders.middleware.CorsPostCsrfMiddleware',
]

WSGI_APPLICATION = 'mysite.wsgi.application'

CORS_ALLOW_METHODS = default_methods + (
    'POKE',
)

CORS_ALLOW_HEADERS = default_headers + (
    'my-custom-header',
)

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

DATABASES = {}

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

AUTH_PASSWORD_VALIDATORS = []

CORS_ORIGIN_ALLOW_ALL = True
# Internationalization
# https://docs.djangoproject.com/en/1.11/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.11/howto/static-files/

STATIC_URL = '/static/'

This is the Traceback I'm getting

Failed to execute script function Traceback (most recent call last):
File "site-packages/django/utils/module_loading.py",
line 23, in import_string AttributeError: module 'django.middleware.common' has no attribute 'CorsMiddleware'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "function.py", line 2, in File "/app/cloudfn/pip-cache-3.5/lib/python3.5/site-packages/PyInstaller/loader/pyimod03_importers.py",
line 631, in exec_module File "mysite/wsgi.py",
line 16, in File "site-packages/django/core/wsgi.py",
line 14, in get_wsgi_application File "site-packages/django/core/handlers/wsgi.py",
line 151, in init File "site-packages/django/core/handlers/base.py",
line 80, in load_middleware File "site-packages/django/utils/module_loading.py",
line 27, in import_string File "site-packages/django/utils/six.py",
line 685, in reraise File "site-packages/django/utils/module_loading.py",
line 23, in import_string ImportError: Module "django.middleware.common" does not define a "CorsMiddleware" attribute/class

@adamchainz
Copy link
Owner

The error is that you've added the path django.middleware.common.CorsMiddleware to your MIDDLEWARE setting, and this doesn't exist. The error message is pretty clear I think! Probably some copy/paste mistakes, this library provides corsheaders.middleware.CorsMiddleware which you have in there correctly, and django.middleware.common.CommonMiddleware is one of Django's built-ins: https://docs.djangoproject.com/en/2.0/topics/http/middleware/ .

Also your settings has both MIDDLEWARE and MIDDLEWARE_CLASSES, MIDDLEWARE is the new one from later versions of Django and MIDDLEWARE_CLASSES will be ignored, you should remove it and make sure you have everything you need in MIDDLEWARE, e.g. the csrf and cors-post middlewares.

HTH

@santhoshdc1590
Copy link
Author

@adamchainz Thank You for the MIDDLEWARE and MIDDLEWARE_CLASSES. Removing the django.middleware.common.CorsMiddleware from MIDDLEWARE did help.

I have django-cors-headers==2.2.0 installed. I am still having this error

Failed to execute script function Traceback (most recent call last):
File "function.py", line 2, in
File "/app/cloudfn/pip-cache-3.5/lib/python3.5/site-packages/PyInstaller/loader/pyimod03_importers.py",
line 631, in exec_module File "mysite/wsgi.py",
line 16, in File "site-packages/django/core/wsgi.py",
ine 14, in get_wsgi_application File "site-packages/django/core/handlers/wsgi.py",
line 151, in init File "site-packages/django/core/handlers/base.py",
line 80, in load_middleware File "site-packages/django/utils/module_loading.py",
line 20, in import_string File "importlib/init.py",
line 126, in import_module ImportError: No module named 'corsheaders.middleware'

@adamchainz
Copy link
Owner

The import failure indicates it's not actually installed. Perhaps you are mixing up python environments.

@santhoshdc1590
Copy link
Author

This is my requirements.txt

django==1.11.1
djangorestframework==3.8.2
django-cors-headers==2.2.0
altgraph==0.15
cachetools==2.0.1
certifi==2018.1.18
chardet==3.0.4
Django==1.11.1
future==0.16.0
google-auth==1.4.1
idna==2.6
Jinja2==2.10
jsonpickle==0.9.6
macholib==1.9
MarkupSafe==1.0
numpy==1.14.2
opencv-python==3.3.0.9
pefile==2017.11.5
pyasn1==0.4.2
pyasn1-modules==0.2.1
pycloudfn==0.1.209
PyInstaller==3.3.1
pyspin==1.1.1
python-dateutil==2.6.0
pytz==2018.3
requests==2.18.4
rsa==3.4.2
scipy==1.0.1
six==1.10.0
urllib3==1.22
Werkzeug==0.12
google-cloud==0.32.0

@adamchainz
Copy link
Owner

I'm sorry, I don't think I can help you any further.

@santhoshdc1590
Copy link
Author

@adamchainz Thank You

@web2cap

This comment was marked as outdated.

Repository owner locked as off-topic and limited conversation to collaborators Jul 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants