django.core.exceptions.ImproperlyConfigured:无法加载WSGI应用程序'{project_name} .wsgi.application';错误导入模块

时间:2019-06-16 18:10:26

标签: django heroku wsgi

我不明白为什么在运行Heroku run python manage.py runserver时会遇到错误

我尝试更改文件夹名称,并尝试在WhiteNoise中删除并添加MIDDLEWARE

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    # 'django.middleware.security.SecurityMiddleware',
    # 'whitenoise.middleware.WhiteNoiseMiddleware',
]

数据库设置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

django_heroku.settings(locals())

wsgi.py

import os

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

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

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

至少服务器应该运行,以便我可以了解Heroku的问题。

2 个答案:

答案 0 :(得分:0)

您的wsgi.py文件不正确。

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{PROJECT}}.settings')

上面的代码就是问题所在,您需要用实际的应用名称替换{{ project }}

settings.py所在的文件夹名称。

例如,如果这是您的项目结构。

blog
      ...
    - settings.py
    - wsgi.py
      ...

然后正确的代码是os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blog.settings')

答案 1 :(得分:0)

根据documentation,中间件配置应如下所示:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

您也可以从wsgi文件中删除与白噪声相关的行:

import os

from django.core.wsgi import get_wsgi_application
#  from whitenoise.django import DjangoWhiteNoise

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

application = get_wsgi_application()
#  application = DjangoWhiteNoise(application)

因为在whitenoise >= 4.0中,您不需要更改wsgi文件。