【问题标题】:Django: Image referenced in CSS showing locally but not on HerokuDjango:在 CSS 中引用的图像在本地显示,但在 Heroku 上没有
【发布时间】: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'

还有here's a picture of the file structure

【问题讨论】:

    标签: css django heroku


    【解决方案1】:

    在您提供的屏幕截图中,文件名是nyc.JPG,而您的代码引用了nyc.jpg。文件名区分大小写:)

    【讨论】:

      【解决方案2】:

      快跑

      python manage.py collectstatic
      

      查看更多:collectstaticDjango and Static Assets

      【讨论】:

      • 她报告了运行collectstatic时出现的错误。
      猜你喜欢
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 2020-12-31
      • 2020-12-25
      • 2019-12-23
      • 2017-11-04
      • 2015-07-04
      • 2017-04-11
      相关资源
      最近更新 更多