Table of Contents
If you have questions about Python on Heroku, consider discussing it in the Python on Heroku forums. Both Heroku and community-based Python experts are available.
Django settings for static assets can be a bit difficult to configure and debug.
However, if you just add the following settings to your
settings.py, everything should work exactly as you expect:
# Static asset configuration import os PROJECT_PATH = os.path.dirname(os.path.abspath(__file__)) STATIC_ROOT = 'staticfiles' STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(PROJECT_PATH, 'static'), )
To serve static files in production, you can use the dj-static library.
$ pip install dj-static ... $ pip freeze > requirements.txt
Next, you can add the following code to
wsgi.py to serve static files in production:
from django.core.wsgi import get_wsgi_application from dj_static import Cling application = Cling(get_wsgi_application())
Your application will now serve static assets directly from Gunicorn in production. This will be perfectly adequate for most applications, but top-tier applications may want to explore using a CDN with Django-Storages.
When a Django application is deployed to Heroku,
collectstatic is run automatically when it is configured properly.
We determine if collectstatic is configured by running the following against your codebase:
$ python manage.py collectstatic --dry-run --noinput
If required configurations are missing, this command will fail and no collectstatic support will be applied to your application.
To learn more about why your application’s collectstatic isn’t configured, you can use
$ heroku run python manage.py collectstatic --noinput ... django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting.
Sometimes, you may not want Heroku to run
collectstatic on your behalf. You can disable collectstatic with the
$ heroku config:set DISABLE_COLLECTSTATIC=1