Skip to content

Deploy a Django app to Heroku

Index:

Requirements:

  • Git installed
  • Python >3.6
  • Heroku CLI (see step 1)

1) Install the Heroku CLI

Install the Heroku Command Line Interface (CLI) for your platform from here. Once installed, you can use the heroku command from your command shell. To log in to the Heroku CLI use:

$ heroku login

2) Deploying Python and Django apps on Heroku

Heroku automatically identifies your app as a Python app if any of the following files are present in its root directory: * Pipfile * setup.py * requirements.txt

When you deploy to Heroku, the dependencies you specify are automatically installed. If you’re using Django, the collectstatic command also runs automatically during the deployment process. For this to work properly make sure you install the Django-Heroku Python package (step 3).

3) Configure Django apps for Heroku

Create a Procfile (no extension) and add the following content where myproject is the name of your Django app.

web: gunicorn myproject.wsgi

Install gunicorn:

$ pipenv install gunicorn

Install the django-heroku package

$ pipenv install django-heroku

Add the following import statement to the top of settings.py:

import django_heroku
Then add the following to the bottom of settings.py:
# Activate Django-Heroku.
django_heroku.settings(locals())

4) Deploy to Heroku

Using the Heroku CLI lets create our app and the database.

$ heroku login
$ heroku create <desired_app_name>

Now lets push our code to Heroku:

$ git add .
$ git commit -m "Ready to heroku this."
$ git push heroku master

Note: If you wish to depoly from a local branch other than master, e.g. testbranch then use:

$ git push heroku testbranch:master

Finally, migrate your Database to the Heroku app:

$ heroku run python manage.py migrate

You should now be able to see your app in the Heroku Dashboard as well as a Dyno web process with the ON indication.

5) Import local database to Heroku (Optional)

If you are using a postgresql database locally you can easily import it to your newly created Heroku app. First create a backup of your local DB:

pg_dump -U USERNAME DATABASE > pg_db_dump.sql

To import it to Heroku run:

heroku pg:psql --app APPNAME < pg_db_dump.sql

References: