Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django staticfiles not found on Heroku (with whitenoise)

Tags:

This question seems to be asked several time but I can not fix it.

I deployed a django app on production with DEBUG = False. I set my allowed_host. I used {% load static from staticfiles %} to load static files. I exactly write the settings sugested by Heroku doc :

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

BUT I got an error 500. And got this traceback (by mail)

...
`cache_name = self.clean_name(self.hashed_name(name))
 File "/app/.heroku/python/lib/python3.5/site-    packages/django/contrib/staticfiles/storage.py", line 94, in hashed_name (clean_name, self))
...
ValueError: The file ‘app/css/font.css’ could not be found with <whitenoise.django.GzipManifestStaticFilesStorage object at 0x7febf600a7f0>.`

When I run heroku run python manage.py collectstatic --noinput All seems ok :

276 static files copied to '/app/annuaire/staticfiles', 276 post-processed.

Does anyone have an idea to help me, please ?

Thanks

EDIT :

annuaire
|-- /annuaire
|-- -- /settings.py
|-- /app
|-- -- /static/...`

wsgi.py

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise


application = get_wsgi_application()
application = DjangoWhiteNoise(application)
like image 307
vpoulain Avatar asked Feb 19 '16 14:02

vpoulain


People also ask

What is WhiteNoise Python?

Radically simplified static file serving for Python web apps. With a couple of lines of config WhiteNoise allows your web app to serve its own static files, making it a self-contained unit that can be deployed anywhere without relying on nginx, Amazon S3 or any other external service.

How do I serve a static file in Heroku?

Try using Amazon s3 for storing static and media files. A similar question and answer can be found here. Here is heroku's documentation of using s3. Then you might want to disable automatic collectstatic instructions here so collectstatic isn't run every time you push to heroku.


2 Answers

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.

like image 157
Zac Kwan Avatar answered Sep 21 '22 22:09

Zac Kwan


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

like image 20
vpoulain Avatar answered Sep 20 '22 22:09

vpoulain