django.core.exceptions.ImproperlyConfigured: 无法加载 WSGI 应用程序 'DjBlog.wsgi.application';导入模块时出错

django.core.exceptions.ImproperlyConfigured: WSGI application 'DjBlog.wsgi.application' could not be loaded; Error importing module

我用 Django 开始了一个网站建设项目,但是 运行 遇到了 WSGI 服务器的问题,但我没有改变任何东西

请知道答案的朋友告诉我。

这是我写的代码

settings.py

INSTALLED_APPS = [
    # 'django.contrib.admin',
    'django.contrib.admin.apps.SimpleAdminConfig',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.contrib.sitemaps',
    'mdeditor',
    'haystack',
    'blog',
    'accounts',
    'comments',
    'oauth',
    'servermanager',
    'owntracks',
    'compressor',
    # 'corsheader',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheader.middleware.CorsMiddleware',
    'django.middleware.gzip.GZipMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    # 'debug_toolbar.middleware.DebugToolbarMiddleware',
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.http.ConditionalGetMiddleware',
    'blog.middleware.OnlineMiddleware'
]

ROOT_URLCONF = 'DjBlog.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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',
                'blog.context_processors.seo_processor'
            ],
        },
    },
]

WSGI_APPLICATION = 'DjBlog.wsgi.application'

wsgi.py

"""
WSGI config for DjBlog project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/
"""

import os


from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_project.settings')

application = get_wsgi_application()

错误

Traceback (most recent call last):
  File "D:\DjangoBlog\venv\lib\site-packages\django\core\servers\basehttp.py", line 45, in get_internal_wsgi_application
    return import_string(app_path)
  File "D:\DjangoBlog\venv\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "D:\DjangoBlog\DjBlog\wsgi.py", line 17, in <module>
    application = get_wsgi_application()
  File "D:\DjangoBlog\venv\lib\site-packages\django\core\wsgi.py", line 13, in get_wsgi_application
    return WSGIHandler()
  File "D:\DjangoBlog\venv\lib\site-packages\django\core\handlers\wsgi.py", line 127, in __init__
    self.load_middleware()
  File "D:\DjangoBlog\venv\lib\site-packages\django\core\handlers\base.py", line 40, in load_middleware
    middleware = import_string(middleware_path)
  File "D:\DjangoBlog\venv\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'corsheader'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\threading.py", line 950, in _bootstrap_inner
    self.run()
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\threading.py", line 888, in run
    self._target(*self._args, **self._kwargs)
  File "D:\DjangoBlog\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper

(venv) D:\DjangoBlog>py manage.py runserver
Watching for file changes with StatReloader
[2020-10-07 16:08:24,084] INFO [django.utils.autoreload.run_with_reloader:613 autoreload] Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
October 07, 2020 - 16:08:26
Django version 3.1.2, using settings 'DjBlog.settings'
Starting development server at http://127.0.0.1:8000/ 
Quit the server with CTRL-BREAK.
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "D:\DjangoBlog\venv\lib\site-packages\django\core\servers\basehttp.py", line 45, in get_internal_wsgi_application
    return import_string(app_path)
  File "D:\DjangoBlog\venv\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked  
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module     
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "D:\DjangoBlog\DjBlog\wsgi.py", line 17, in <module>
    application = get_wsgi_application()
  File "D:\DjangoBlog\venv\lib\site-packages\django\core\wsgi.py", line 13, in get_wsgi_application
    return WSGIHandler()
  File "D:\DjangoBlog\venv\lib\site-packages\django\core\handlers\wsgi.py", line 127, in __init__
    self.load_middleware()
  File "D:\DjangoBlog\venv\lib\site-packages\django\core\handlers\base.py", line 40, in load_middleware
    middleware = import_string(middleware_path)
  File "D:\DjangoBlog\venv\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'corsheader'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\threading.py", line 950, in _bootstrap_inner
    self.run()
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\threading.py", line 888, in run
    self._target(*self._args, **self._kwargs)
  File "D:\DjangoBlog\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "D:\DjangoBlog\venv\lib\site-packages\django\core\management\commands\runserver.py", line 138, in inner_run
    handler = self.get_handler(*args, **options)
  File "D:\DjangoBlog\venv\lib\site-packages\django\contrib\staticfiles\management\commands\runserver.py", line 27, in get_handler     
    handler = super().get_handler(*args, **options)
  File "D:\DjangoBlog\venv\lib\site-packages\django\core\management\commands\runserver.py", line 65, in get_handler
    return get_internal_wsgi_application()
  File "D:\DjangoBlog\venv\lib\site-packages\django\core\servers\basehttp.py", line 47, in get_internal_wsgi_application
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: WSGI application 'DjBlog.wsgi.application' could not be loaded; Error importing module.   

我已经搜索并尝试了很多方法,但我找不到合适的方法。

wsgi.py 文件中将 django_project.settings 更改为 DjBlog.settings

然后按照以下步骤克服 ModuleNotFoundError。

第 1 步:安装应用程序 django-cors-headers

pip install django-cors-headers

第 2 步:将 corsheader 添加到 settings.py

中的已安装应用程序列表
INSTALLED_APPS = [
    ...
    'corsheaders',
    ...
]

第 3 步:将 CorsMiddleware 和 CommonMiddleware 添加到 settings.py

中的中间件列表
MIDDLEWARE = [
    ...
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
]

第 4 步:只需在 settings.py 中添加以下变量即可允许访问所有域:

ALLOWED_HOSTS=['*']

CORS_ORIGIN_ALLOW_ALL = True

更多:https://dzone.com/articles/how-to-fix-django-cors-error

在你的wsgi.py中替换:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_project.settings')

与:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'DjBlog.settings').

我也遇到了同样的问题(文件“<frozen importlib._bootstrap>”)

解决方案:

  1. python manage.py migrate
  2. python manage.py collectstatic(如果项目中需要静态)。

执行以上命令后,我们就可以运行项目成功了。