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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.