我想使用Heroku's documentation在我的本地机器和生产环境中提供静态文件。然而,每当我运行我的应用程序debug=True
一切都按预期工作;静态文件将被检索并且应用按预期显示。但是,每当我更换debug=False
时,我都会收到Server Error (500)
。据我可以告诉这一切归结到我的STATICFILE_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
。当我发表评论时,我的应用程序将以debug=False
运行,但由于缺少静态文件而没有样式。我已经通过了whitenoise documentation,heroku's,但我不知道是怎么回事,除了STATICFILE_STORAGE
是问题。白化不适合生产吗?我必须在生产中使用CDN吗?这是一个小时间的应用程序,所以我希望不必使用CDN,但如果有必要的话。在heroku上使用whitenoise的Django静态文件
settings.py(我只包括我认为是相关settings.py的部分。如果您需要整个事情,让我知道。)
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
MIDDLEWARE = [
'django_hosts.middleware.HostsRequestMiddleware',
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
.......
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MySite.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
日志
2017-04-14T08:40:06.800390+00:00 heroku[router]: at=info method=GET path="/" host=wgsite.herokuapp.com request_id=6bf564f7-efba-42fb-b8b1-970a02a5283a fwd="5.51.58.217" dyno=web.1 connect=0ms service=11ms status=302 bytes=223 protocol=https
2017-04-14T08:40:06.997956+00:00 heroku[router]: at=info method=GET path="/login" host=wgsite.herokuapp.com request_id=4ce4486e-7e06-4fcd-a562-88ec3ffd8fa9 fwd="5.51.58.217" dyno=web.1 connect=0ms service=6ms status=302 bytes=243 protocol=https
在此先感谢您的帮助!
UPDATE:
因此,原来我不得不跑heroku config:unset DISABLE_COLLECTSTATIC
获得Heroku的自动collectstatic。现在,它扔我这些错误:
remote: ! Error while running '$ python manage.py collectstatic --noinput'.
remote: See traceback above for details.
这是奇怪的,因为我可以成功在本地运行的python manage.py collectstatic ...
更新#2
我跑collectstatic
本地和一个名为“staticfiles”的目录是由我的所有静态文件由app内部组织的。我推送到heroku,现在我的网站打开,所有静态文件运行debug=False
。我仍然无法让heroku在没有出错的情况下自动收集静态数据。
什么是从日志完整的错误? –
如果你使用whitenoise,你不需要cdn,我一直在Heroku的静态文件中使用它。你有任何错误日志信息? – dentemm
我在上面的第一个问题中添加了日志。 – grigs