Django:在CSS中引用的图像显示在本地但不在Heroku上

时间:2021-05-03 23:02:16

I'm working in Django, deploying via Heroku, trying to use WhiteNoise for static assets.

我在Django工作,通过Heroku部署,试图将WhiteNoise用于静态资产。

I have a CSS file which refers to an image to use as a background:

我有一个CSS文件,它引用一个用作背景的图像:

body { background: white url("images/nyc.jpg") left top; }

When I load the page locally, the background image shows up perfectly.

当我在本地加载页面时,背景图像显示完美。

However, when I deploy to Heroku (using git push from my local machine), the background image does NOT show up on the page. I get this error message when I do the git push to Heroku:

但是,当我部署到Heroku时(使用本地计算机上的git push),背景图像不会显示在页面上。当我对Heroku执行git推送时收到此错误消息:

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.

Here are the relevant lines from my project level settings.py (happy to share more from it if needed):

以下是我的项目级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'

And here's a picture of the file structure.

这是文件结构的图片。

2 个解决方案

#1


0  

Just run

python manage.py collectstatic

see more : collectstatic , Django and Static Assets

看更多:collectstatic,Django和Static Assets

#2


0  

In the screenshot you provided, the filename is nyc.JPG, while your code references nyc.jpg. Filenames are case-sensitive :)

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

#1


0  

Just run

python manage.py collectstatic

see more : collectstatic , Django and Static Assets

看更多:collectstatic,Django和Static Assets

#2


0  

In the screenshot you provided, the filename is nyc.JPG, while your code references nyc.jpg. Filenames are case-sensitive :)

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