Handling static files

At one point or another you want to add assets like CSS files, JavaScript files or images to your project.

The adviced solution is to store the files externally (like an AWS S3 bucket). For prototypes or smaller projects it is easier to let Django serve the static files on production as well. While Django handles static files locally just fine, the Heroku app needs a bit of configuration.

Add django-heroku

With django-heroku Heroku provides a handy Python package that helps dealing with configuration settings for Heroku. Especially the handling of settings, static files and logging is much easier with it.

PIP grabs all required dependencies when installing django-heroku. This includes:

  • dj-database-url`

  • whitenoise

  • psycopg2

  • django (We already have this one! ๐Ÿ˜‰)

Install django-heroku:

Terminal
pip install django-heroku

Add it to our settings.py file:

djangoku/settings.py (At the bottom of the file)
import django_heroku
django_heroku.settings(locals())

Update our requirements

Now that we have two new modules in our project, letโ€™s update our requirements.

Update requirements.txt:

Terminal
pip freeze > requirements.txt

Commit the code

If you run git status you will see, that we updated requirements.txt and settings.py.

Now we can stage and commit our changes:

Terminal
git add .
git commit -m "Configure static files handling ๐Ÿ“‚"

Checklist

โœ”๏ธŽ Collectstatic works

โœ”๏ธŽ django-heroku is part of requirements

results matching ""

    No results matching ""