【问题标题】:Django ImportError: No module named middlewareDjango ImportError:没有名为中间件的模块
【发布时间】:2015-11-20 17:18:27
【问题描述】:

我使用的是 Django 1.8 版和 python 2.7。运行项目后出现以下错误。

Traceback (most recent call last):
  File "C:\Python27\lib\wsgiref\handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "C:\Python27\lib\site-packages\django\contrib\staticfiles\handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "C:\Python27\lib\site-packages\django\core\handlers\wsgi.py", line 170, in __call__
    self.load_middleware()
  File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 50, in load_middleware
    mw_class = import_string(middleware_path)
  File "C:\Python27\lib\site-packages\django\utils\module_loading.py", line 26, in import_string
    module = import_module(module_path)
  File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named middleware
[26/Aug/2015 20:34:29] "GET /favicon.ico HTTP/1.1" 500 59

这是我的 settings.py 文件

"""
Django settings for collageapp project.

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

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

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

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

APP_PATH = os.path.dirname(os.path.abspath(__file__))
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.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '******************************************************'

# 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',
    'manageApp',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.facebook',
    'allauth.socialaccount.providers.google',
    'django.contrib.admindocs',
    'rest_framework',
)

SITE_ID = 1

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'oauth2_provider.middleware.OAuth2TokenMiddleware',
)


ROOT_URLCONF = 'collageapp.urls'
CORS_ORIGIN_ALLOW_ALL = True

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',
                'allauth.account.context_processors.account',
                'allauth.socialaccount.context_processors.socialaccount'
            ],
        },
    },
]

WSGI_APPLICATION = 'collageapp.wsgi.application'


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

DATABASES = {
     'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'college_app',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}



# Internationalization
# https://docs.djangoproject.com/en/1.8/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.8/howto/static-files/

STATIC_URL = '/static/'

我已经尝试浏览错误但徒劳无功。我的相同代码在其他机器上运行良好。

【问题讨论】:

  • 这个错误和sql文件迁移或者workspace.xml有关系吗?

标签: python django django-settings django-middleware


【解决方案1】:

确保在可以找到它们的地方安装了所有支持包。我遇到了安装多个 Python 解释器的问题,并且在使用另一个解释器安装软件包时无意中使用一个解释器运行 Django。我要验证的另一件事是您在两台机器上都有相同版本的软件包。请密切注意corsheaders.middleware.CorsMiddlewareoauth2_provider.middleware.OAuth2TokenMiddleware,因为它们不是 Django 的一部分。

【讨论】:

  • 我已经安装了所有这些软件包,但仍然收到错误消息。我正在使用 django 1.7。以下是我的冻结列表: defusedxml==0.4.1 Django==1.7 django-cors-headers==1.3.1 django-tastypie==0.13.3 lxml==3.6.4 oauth2-provider==0.0 Pillow==3.4 .2 python-dateutil==2.6.0 python-mimeparse==1.6.0 六==1.10.0 South==1.0.2 (env) mike@mike-VirtualBox:/var/www/CordeliaHanelBackend-master$跨度>
  • 你应该发布一个新问题。
【解决方案2】:

在你的项目目录中运行python manage.py shell打开一个python shell。

在 python shell 中一次一个运行以下命令:

>>> from corsheaders.middleware import CorsMiddleware
>>> from oauth2_provider.middleware import OAuth2TokenMiddleware
>>> from django.contrib.auth.middleware import SessionAuthenticationMiddleware

其中一行应该会给您如下错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named middleware

给你这个错误的那一行是给你带来问题的缺失模块。

要查找搜索模块的路径,请执行以下操作

>>> import sys; sys.path

或者,如果您不知道如何使用 python shell,您可以一次删除 settings.MIDDLEWARE_CLASSES 中的以下行,直到您不再收到该错误:

'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'corsheaders.middleware.CorsMiddleware',
'oauth2_provider.middleware.OAuth2TokenMiddleware',

只需重新安装出现错误的软件包即可。

django.contrib.auth.middleware -> django

corsheaders.middleware -> corsheaders

oauth2_provider.middleware -> oauth2_provider

【讨论】:

  • 感谢您提供解决问题的“理智”方法。我有一个类似的,你让我很开心:)
  • @PaoloStefan 很高兴它有帮助:)
【解决方案3】:

对于我的情况,我做了以下:

  1. 检查 settings.py 文件
  2. 检查中间件部分是否有任何可能已被删除、意外删除和纠正的部分。

就我而言,以下内容已被删除:

django.middleware.security.SecurityMiddleware

【讨论】:

    猜你喜欢
    • 2012-10-21
    • 1970-01-01
    • 2021-01-28
    • 2020-09-07
    • 2018-02-05
    • 2015-07-20
    • 2018-07-28
    • 2023-03-27
    • 2015-08-20
    相关资源
    最近更新 更多