简体   繁体   中英

Django Heroku not serving static files when Debug=False

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 .

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.

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

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