【发布时间】:2016-05-09 08:59:09
【问题描述】:
我在 Django 中工作,通过 Heroku 进行部署,尝试将 WhiteNoise 用于静态资产。
我有一个 CSS 文件,它引用了用作背景的图像:
body { background: white url("images/nyc.jpg") left top; }
当我在本地加载页面时,背景图像完美显示。
但是,当我部署到 Heroku(使用本地计算机上的 git push)时,背景图像不会显示在页面上。当我对 Heroku 执行 git push 时收到此错误消息:
Traceback (most recent call last):
whitenoise.django.MissingFileError: The file 'multiblog/images/nyc.jpg' could not be found with <whitenoise.django.GzipManifestStaticFilesStorage object at 0x7fab5c4fb210>.
The CSS file 'multiblog/style.css' references a file which could not be found:
Please check the URL references in this CSS file, particularly any
relative paths which might be pointing to the wrong location.
以下是我的项目级别 settings.py 中的相关行(如果需要,很高兴从中分享更多内容):
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
BASE_DIR2 = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR2, 'static'),
]
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
【问题讨论】: