简体   繁体   中英

Unable to find static files in production with Django using whitenoise

Whenever I've deployed my Django app on Heroku, my browser doesn't load the css and images. When I open the console in chrome it says:

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

I have:

  • whitenoise 3.3.0 include in my requirements.txt
  • whitenoise added as a middleware in my settings.py
  • Added whitenoise to wsgi.py
  • used "python manage.py collectstatic" on both my local computer and on heroku bash

My settings.py looks like this (at the bottom of the file:)

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'),
)

My file structure looks like this:

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

EDIT:

This is how I added whitenoise to my wsgi.py and middleware:

Middleware:

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

Not sure if you solved this, but with the lastest docs it says that no whitenoise calls should appear in your wsgi.py file.

Read more here: http://whitenoise.evans.io/en/stable/changelog.html#v4-0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM