簡體   English   中英

無法使用Whitenoise在Django中的生產中找到靜態文件

[英]Unable to find static files in production with Django using whitenoise

每當我在Heroku上部署Django應用程序時,我的瀏覽器就不會加載CSS和圖像。 當我在Chrome中打開控制台時,它說:

GET https://MY_URL.com/static/MY_APP/images/MY_PICTURE.jpg 404 (Not Found)

我有:

  • whitenoise 3.3.0包含在我的requirements.txt中
  • 在我的settings.py中將whitenoise添加為中間件
  • 向wsgi.py添加了whitenoise
  • 在我的本地計算機和heroku bash上都使用了“ python manage.py collectstatic”

我的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

編輯:

這是我在wsgi.py和中間件中添加whitenoise的方式:

中間件:

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())

不知道您是否解決了這個問題,但是使用最新的文檔說,wsgi.py文件中不應出現白噪聲調用。

在此處閱讀更多信息: http : //whitenoise.evans.io/en/stable/changelog.html#v4-0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM