每当我在Heroku上部署我的Django应用程序时,我的浏览器都不会加载css和图像。 当我用chrome打开控制台时,它说:
GET https://MY_URL.com/static/MY_APP/images/MY_PICTURE.jpg 404 (Not Found)
我有:
我的settings.py看起来像这样(在文件的底部:)
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
PROJECT_ROOT2 = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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_ROOT2, 'static'),
)
我的文件结构如下:
APP_FOLDER
WEBSITE_FOLDER
> settings
> > local.py
> > production.py
> static
> > [useless css file to make directory visible to git]
staticfiles
> admin
> APP_NAME
> > images
> > > image1.jpg
> > > image2.jpg
> > style.css
编辑:
这就是我将whitenoise添加到我的wsgi.py和中间件中的方式:
中间件:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "failcounter.settings")
application = DjangoWhiteNoise(get_wsgi_application())
答案 0 :(得分:0)
不确定是否解决了此问题,但是使用最新的文档说,wsgi.py文件中不应出现白噪声调用。
在此处了解更多信息:http://whitenoise.evans.io/en/stable/changelog.html#v4-0