Link to main pageMain page 🏡

How to deploy your Django application to Heroku

A small and straightforward guide
Posted on 2020-04-29 Edited on 2020-05-01
10 mins read

Photo by Pero Kalimero on Unsplash

Table of contents


Deploying a Django app to production for the first time is not the easiest thing in this world.

In order to make this process more clear and less painful, I made this guide as small as possible to successfully deploy your app without losing the understanding of how it works.

Here is the basic workflow of the Django web-application in production:

[user's web-browser] <--> [web-server] <--> [wsgi-server] <--> [your app's code]

Let's roll 🚴‍♀️

App prep

  1. In your app's python environment install Gunicorn. It's a light-weight pure-Python WSGI(web server gateway interface)-server, a gateway between web-server and your application's code.
$ pip install gunicorn

Another WSGI-server's options you can get here

  1. There are several flavors to free host your Django app. The most popular nowadays are Amazon AWS, Pythonanywhere, and Heroku. Our choice today will be Heroku as it is specialized at web-apps and brings you an opportunity to deploy a production/dev/staging environment almost in a couple of clicks.

So let's pip install a utility that automatically configures your Django app to work with Heroku:

$ pip install django-heroku

import to your Django app's settings ( in the default case):

import django_heroku

and activate Django-Heroku config at the end of the same settings-file:

  1. In your app's root folder create runtime.txt file with just one string, representing your Python's version python-3.7.6
  2. Next, create Procfile in the same directory with the string declaring how to deploy your app
web: gunicorn project_name.wsgi --log-file -

where project_name is obviously your Django project's name

  1. Now let's review our Django with "security, performance, and operations in mind".
  2. Make sure that your SECRET_KEY used in production isn't mentioned anywhere else and is not committed to a version control system.
  3. Set

    DEBUG = False


    ALLOWED_HOSTS = ['localhost', '', '[::1]']
  4. Explicitly set sensitive data cookies transmitting over HTTPS:

  5. Now you can run

    python check --deploy

    command and check if you can apply another security/performance tweaks, recommended by Django, to your app. These tweaks are different for each specific app.

  6. Add

to to see Django's errors in Heroku logs

Heroku setup

  1. Signup to Heroku
  2. Download/install Git if you didn't have it already
  3. Download/install Heroku CLI (if path error then add to path manually)
  4. After Heroku CLI setup opens new Terminal/Command Prompt window, go to your Django project's path and login to Heroku:
$ heroku login
  1. If you didn't have a Git repository initialized in your Django project's root dir, initialize it:
$ git init

add *.pyc string to .gitignore file (create if you don't have it yet), commit changes to local repo:

$ git add *
$ git commit -m 'initial'
  1. Create your Heroku app

First of all you have to choose the region where you want to deploy the app:

$ heroku regions
ID         Location                 Runtime
─────────  ───────────────────────  ──────────────
eu         Europe                   Common Runtime
us         United States            Common Runtime
dublin     Dublin, Ireland          Private Spaces
frankfurt  Frankfurt, Germany       Private Spaces
oregon     Oregon, United States    Private Spaces
sydney     Sydney, Australia        Private Spaces
tokyo      Tokyo, Japan             Private Spaces
virginia   Virginia, United States  Private Spaces
$ heroku create app-name --region eu --buildpack heroku/python

Your app's url will be something like this

Git repo

When you create an app, a Git remote (called heroku) is also created and associated with your local Git repository.

  1. Deploy the app to Heroku:
$ git push heroku master
  1. Migrate the database:
$ heroku run python migrate
  1. Create the superuser:
$ heroku run python createsuperuser
  1. Optionally: if you have a "Missing staticfiles manifest entry for 'favicon.ico'" error on Django admin page bundle static files locally:
$ python collectstatic

add this to


and push changes to Heroku:

$ git push heroku master

Now you can watch your app in the browser:

$ heroku open

🎉 Congratulations! Your app's web-debut had happened successfully !🍹