【发布时间】:2020-01-23 00:03:13
【问题描述】:
我正在尝试使用 Heroku 部署我的第一个 React DRF 应用程序。它已成功部署(在 Heroku Dashboard 中查看发布日志下,显示没有要应用的迁移)。但是当我尝试访问该网站时,它给了我错误提示
Invalid HTTP_HOST header: 'mySite-test.herokuapp.com'. You may need to add 'mySite-test.herokuapp.com' to ALLOWED_HOSTS.
虽然我在 settings.py 中添加了相同的内容。我正在使用 gunicorn 版本 19.9.0 和 whitenoise 版本 4.1.3 。
到目前为止我做了什么
import os
import django_heroku
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True
ALLOWED_HOSTS = ['http://127.0.0.1:8000/', 'https://mySite-test.herokuapp.com/']
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'personal_blog.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'build')],
'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',
],
},
},
]
WSGI_APPLICATION = 'personal_blog.wsgi.application'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'build/static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
django_heroku.settings(locals())
wsgi.py
import os
from whitenoise import WhiteNoise
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'personal_blog.settings')
application = get_wsgi_application()
application = WhiteNoise(application, root='build/static')
在项目根 urls.py 中
from django.views.generic import TemplateView
urlpatterns = [
path('', include('accounts.api.urls')),
re_path('.*', TemplateView.as_view(template_name='index.html'))
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这是我的文件夹结构
请有人指出我在这里做错了什么。
【问题讨论】:
标签: reactjs heroku django-rest-framework