我想使用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