3

I'm hosting my Django application on Heroku and using whitenoise to handle serving static files.

Following is content of settings.py

DEBUG = False

ALLOWED_HOSTS += [
    'example.com',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ...
]

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static_my_project')
]

STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn', 'static_root')

But static files are not working.

Setting Debug=True is serving static files but not when Debug=False.

Anuj TBE
  • 9,198
  • 27
  • 136
  • 285

2 Answers2

11

Got the solution from a post

Added collectstatic to Procfile

web: python manage.py collectstatic --no-input; gunicorn myapp.wsgi --log-file - --log-level debug

And now every static file is serving including, CSS, js, images and videos.

Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
1

The Whitenoise middleware should come after the security middleware and before all other middleware. You are currently adding it to the end.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • If you’ve tried that then please update your original question to show the current code. Make sure you have committed and pushed the changes. – Alasdair Jan 06 '19 at 13:48