Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Streamline Django and Heroku Configuration

For more information on concepts covered in this lesson, you can check out:

Here are the command-line snippets used in this lesson:

Shell
$ source venv/bin/activate
$ python -m pip install django-heroku
$ python -m pip freeze > requirements.txt
$ cat requirements.txt
$ vim portfolio/settings.py
$ python -c 'import secrets; print(secrets.token_urlsafe())'
$ heroku config:set SECRET_KEY='<your-secret>'
$ heroku config
$ heroku config:unset DISABLE_COLLECTSTATIC
$ git status
$ git commit -am "Install and use django-heroku"
$ git push heroku
$ heroku open

Python Requirements
asgiref==3.4.1
dj-database-url==0.5.0
Django==3.2.7
django-heroku==0.3.1
psycopg2==2.9.2
pytz==2021.3
sqlparse==0.4.2
whitenoise==5.3.0

Python
# ...

# Remove or comment out the hardcoded secret key:
# SECRET_KEY = 'django-insecure-3!1cb-qaq$f71ex!$t#ws(+^w$fx%pifq13dc!qow2ui!4^$^g'

# ...

# Let django-heroku set the allowed hosts:
ALLOWED_HOSTS = []

# ...

# Add this at the bottom of your settings file:
import django_heroku
django_heroku.settings(locals())

00:00 You’ve seen that configuring a Django project for hosting in the cloud can be tedious and error-prone. Fortunately, there are ways to streamline this process.

00:09 In this lesson, you’ll take a closer look at a convenience library called django-heroku, which takes care of the configuration and more. Before you dive in, here’s a quick disclaimer.

00:22 Even though Heroku recommends using it, the last version of django-heroku was released in March of 2018. The library itself is no longer maintained, and the corresponding GitHub repository was archived. Other than that, it depends on a rather heavy-weight driver for the PostgreSQL database, even if you don’t use it. So if you’re concerned about all that, then you can check out an alternative way to configure your Django project for Heroku described by Eric Matthes in his blog.

00:56 Otherwise, activate your virtual environment as usual and install django-heroku.

01:12 Remember to always freeze your dependencies and update the requirements file when you install, upgrade, or remove third-party modules. Otherwise, Heroku won’t know if it needs to pull them and update its cache.

01:25 Next, you’ll need to enable the automatic configuration. So open up the portfolio project settings, scroll down to the bottom of the file, import the newly installed module, and call the settings() function with all the local variables in the current namespace.

01:50 That will automatically populate the list of allowed hosts with the public address of your Heroku app, so you don’t have to hard-code it anymore. You may remove the domain that you manually added earlier.

02:08 In a similar way, django-heroku detects a SECRET_KEY environment variable and uses it to configure Django for you. It lets you avoid keeping the secret key in plaintext, which you could commit to Git by mistake. So, go ahead and remove that line, then save the settings file and generate a new pseudorandom secret key.

02:29 There are a few ways to do that. For example, you can run a Python one-liner using the python -c option, grab the built-in secrets module, and print a unique token.

02:46 Each time you run this command, you’ll get a completely different value. Pick one and use the Heroku CLI to set an environment variable called SECRET_KEY in your remote app.

03:05 To reveal all the environment variables currently defined in your Heroku app, type heroku config. Do you remember that collectstatic command that you had to disable before?

03:17 Well, it turns out that django-heroku can deal with that, too. It leverages the whitenoise library, which simplifies and automates serving static content in Django efficiently.

03:28 In fact, whitenoise was designed with Heroku and Django in mind. So you can unset DISABLE_COLLECTSTATIC now.

03:42 That will leave you with only two environment variables. There’s one you’ve just defined for the secret key, and another one provided by Heroku after your first successful deployment.

03:54 It contains the address of a relational database that Heroku provisions at no charge. I’ll show you how to take advantage of it in a later lesson. Let’s commit and push your streamlined configuration, which uses django-heroku under the surface.

04:22 When the build is complete, open your app to verify if it’s still working as expected. All right, things are looking good. In the next lesson, you’ll see how to tweak the configuration even more.

Become a Member to join the conversation.