我不明白为什么在运行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())
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的问题。
答案 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文件。