Heroku Django Cheat Sheet

Mihai Chelaru-Centea

July 23, 2018

This is a cheat sheet for deploying Python Django apps on Heroku. It contains a list of useful commands, as well as requirements taken from the Getting Started on Heroku with Python Tutorial.

Setup and Required packages

Install pipenv in order to create virtual environments for deploying Heroku apps on the site or locally for debugging/development.

pip install pipenv

Once you have pipenv installed, you can use it to install the following requirements:

pipenv install

Heroku commands

These use the heroku CLI to do various things like create an app, deploy code, get information about the database, etc.

heroku create [APP] creates an app with a custom name. If you omit the [APP] argument then it will randomly generate one for you.

git push heroku master is used to push the git repo of your project to heroku and install all requirements from your Pipfile.

You can scale up the app with heroku ps:scale web=1 to get a working web process.

heroku open opens up your website in a browser. It's a neat shortcut for opening up your app to take a look.

heroku logs prints out the logs from all your streams in a time-ordered fashion. You can use --tail to make the output more manageable and only lok at the last few.

Running locally on Windows

To run the app locally, run the following commands in order:

python manage.py collectstatic collects static assets to the STATIC_ROOT directory for deployment.

In the Heroku CLI run heroku local web -f Procfile.windows and watch the server start up. It may not have the output shown on the heroku guide, but nonetheless it will be working. I got confused and sat there waiting for it to do something thinking that it was hanging for some reason.

Running it locally means that making changes to the app source will also affect the local version of the app. This is great for testing purposes. When you're happy with the results and wish to deploy them again to Heroku's servers, run the following git commands:

git add .
git commit -m "Demo"
git push heroku master
heroku open

Provisioning add-ons

Adding add-ons is super easy. Run heroku addons:create <add-on name> to add a new add-on. Note that this requires account verification.

You can list all your add-ons with heroku addons

To open the add-on, just run heroku addons:open <add-on name>

Define config vars

For Django apps, much of the important configuration settings are stored in the settings.py file, but for heroku they can be defined as config vars, which at runtime are exposed to the application as environment variables.

Use the following command to set an environment variable named TIMES equal to 2 in your Heroku Dyno:

heroku config:set TIMES=2

You can then view all the config vars that you've set using heroku config.

Configuring the DB

To configure a database, in this case PostgreSQL, you need to make sure to use manage.py migrate.

You can run this command on Heroku using heroku run python manage.py migrate.

Navigating to the /db URL route will show a simple report.

The only configuration necessary in settings.py is properly setting the DATABASE_URL environment variable:

# Parse database configuration from $DATABASE_URL
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500, require_ssl=True)

Use heroku pg:psql to log into the database remotely and make queries and do other things as a user. For the getting started with python app, you can simply run select * from hello_greeting; to get all the rows from the /db URL messages.


This is a collection of issues I've run into and the solutions I've come up with to solve them.

Pipfile.lock out of date

After updating the Pipfile you may find that heroku complains about the Pipfile.lock being out of date when you run git push heroku master.

No matter, just run pipenv lock, and then be sure to run git add . to add this changed file to your git repo, then create a new commit to document that change so you have something to push with git commit -m "updated Pipfile.lock".

Now you're free to run git push heroku master and voila, you've deployed the updated version of your app.