Django 不正确配置:无法加载 WSGI 应用程序 'myproject.wsgi.application';导入模块时出错

Django ImproperlyConfigured: WSGI application 'myproject.wsgi.application' could not be loaded; Error importing module

我几乎全新安装了 django,当我尝试 python manage.py runserver.It 时出现了这个错误:

ImproperlyConfigured: WSGI application 'myproject.wsgi.application' could not be loaded; Error importing module.

settings.py

WSGI_APPLICATION = 'myproject.wsgi.application'

wsgi.py

import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
application = get_wsgi_application()

检查堆栈跟踪 - 您可能会在 "The above exception was the direct cause of the following exception:"

行上方几行找到答案

这可能是由于使用了一些未安装的第三方应用的中间件等引起的

注释掉

#'django.contrib.auth.middleware.SessionAuthenticationMiddleware',

在您的 settings.py 中间件

文件中

我遇到了同样的问题,因为我将 debug_toolbar 中间件添加到我的 settings.py

'debug_toolbar.middleware.DebugToolbarMiddleware',

我通过删除 debug_toolbar 中间件解决了这个问题。我还必须从我安装的应用程序中删除 debug_toolbar。

勾选settings.py,

MIDDLEWARE=[
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

删除 'whitenoise.middleware.WhiteNoiseMiddleware', 或安装 Whitenoise (pip install whitenoise)

根据我的经验,当我尝试执行 runserver 但我没有在 [=16= 中安装所有自定义 MIDDLEWARE 时会发生这种情况].识别并安装中间件后,错误得到解决。

对于whitenoise version 4.0或以上: - Django 的 WSGI 集成选项(涉及编辑 wsgi.py)已被删除。相反,您应该将 WhiteNoise 添加到 settings.py 中的中间件列表,并从 wsgi.py.

中删除对 WhiteNoise 的任何引用
MIDDLEWARE = [
  'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',
  # ...
]
  • 'whitenoise.django.GzipManifestStaticFilesStorage' 别名现已删除。相反,您应该使用正确的导入路径:'whitenoise.storage.CompressedManifestStaticFilesStorage'.

出现此问题的原因之一是,如果您在 settings.py 的 django 中间件列表中添加了一个中间件,但尚未安装它。

在我的例子中,我添加了 corsheaders.middleware.CorsMiddleware 而不是 installed.I 使用 pip install django-cors-headers 安装了它并且成功了。

对于遇到同样问题的任何人。我只是按照说明 here

修复了它

您应该将 WhiteNoise 添加到 settings.py 中的中间件列表中,并从 wsgi.py 中删除对 WhiteNoise 的任何引用。

我有同样的错误,在我的例子中,我制作了一个自定义中间件,然后将它添加到 MIDDLEWAREsettings.py 的中间件列表中。问题出在我用于在列表中列出中间件的外壳中。我更改了它以匹配我定制的外壳并且成功了!