Django staticfiles not found on Heroku (with whitenoise)
With DEBUG=False
, what originals use to work does not work for me anymore.
However a fix by enabling whitenoise
on MIDDLEWARE
in settings.py
solved it. Best to be just below SecurityMiddleware
.
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', # add this line #Other middleware...]
```
According to the docs, it actually needs to be enabled in the first place.
I got it. I needed to add
python manage.py collectstatic --noinput;
in my Procfile. Heroku doc said that collecticstatic
is automatically triggered.https://devcenter.heroku.com/articles/django-assets
Thanks
For me following worked.
settings.py
DEBUG = TrueSTATIC_URL = '/static/'STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') #this is not used# Add static folder to STATIC_DIRSSTATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'),]
urls.py
from django.conf.urls.static import staticfrom django.conf import settingsurlpatterns = [] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Note
This helper function works only in debug mode and only if the given prefix is local (e.g. /static/) and not a URL (e.g. http://static.example.com/).
Also this helper function only serves the actual STATIC_ROOT folder; it doesn’t perform static files discovery like django.contrib.staticfiles.