DEV Community

Cover image for How to Deploy Django Applications on Heroku
It's pronounced W3SHY
It's pronounced W3SHY

Posted on

How to Deploy Django Applications on Heroku

Install Heroku CLI

Sign Up to Heroku

After installing the Heroku CLI, open a terminal and login to your account:

$ heroku login
heroku: Press any key to open up the browser to login or q to exit: 
heroku: Waiting for login... ⣻
Enter fullscreen mode Exit fullscreen mode

You're redirected to your browser and you login to Heroku with Heroku CLI and get a success message on your terminal

Creating App

First make sure you are in the root directory of the repository you want to deploy

Next create the heroku app from the terminal:

heroku create <your-app>
Enter fullscreen mode Exit fullscreen mode

Preparing the Application


  • You're familiar with the basics of Django. eg. concept of apps, settings, urls, basics of databases.
  • You have a Django application that you want to deploy to Heroku.
  • You are familiar with virtual environments
  • Your deployment Database is PostgreSQL

You will have to install several packages that will come in handy:

  • django-heroku pip install django-heroku
  • gunicorn pip install gunicorn
  • decouple pip install python-decouple
  • DATABASE_URL pip install dj-database-url
  • whitenoise pip install whitenoise

You will then add the following files in your application

  • Add a Procfile in the project root;
  • Add requirements.txt file with all the requirements in the project root;
  • Add requirements.txt with pip freeze > requirements.txt
  • A runtime.txt to specify the correct Python version in the project root;
  • Configure whitenoise to serve static files.

Now we go through each one of them


Heroku apps include a Procfile that specifies the commands that are executed by the app’s dynos.
Create a file named Procfile in the project root with the following content:

web: gunicorn your_project_name.wsgi
Enter fullscreen mode Exit fullscreen mode

This file contains the python version you are using for heroku to use, create runtime.txt in your project root and add your python version in the following format:

Enter fullscreen mode Exit fullscreen mode

You can specify which runtime to use for your app. List of Heroku Runtimes

Configuring Whitenoise: Django Static Files settings

It turns out django does not support serving static files in production. However, WhiteNoise project can integrate into your Django application, and was designed with exactly this purpose in mind.

Lets first configure static related parameter in

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Static files (CSS, JavaScript, Images)

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
    os.path.join(BASE_DIR, 'static'),
Enter fullscreen mode Exit fullscreen mode

Then add this line of code in the middleware section

    # Simplified static file serving.
Enter fullscreen mode Exit fullscreen mode

Add the following setting to in the static files section to enable gzip functionality.

# Simplified static file serving.


# configuring the location for media
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# Configure Django App for Heroku.
Enter fullscreen mode Exit fullscreen mode

python-decouple and dj-database-url

Python Decouple is a must have app if you are developing with Django. It’s important to keep your application credentials like API Keys, tokens and passwords.
dj-database-url is a simple Django utility that holds your Postgres database url


Firts create a .env file and add it to .gitignoreso you don’t commit any sensitive data to your repository. Below is an example of configurations you can add to the .env file.

# An example, don't share your.env settings
ALLOWED_HOSTS='<app name in heroku>'
Enter fullscreen mode Exit fullscreen mode

We then edit to enable decouple to use the .env configurations.

import os
import django_heroku
import dj_database_url
from decouple import config,Csv

MODE=config("MODE", default="dev")
DEBUG = config('DEBUG', default=False, cast=bool)
# development
if config('MODE')=="dev":
       'default': {
           'ENGINE': 'django.db.backends.postgresql_psycopg2',
           'NAME': config('DB_NAME'),
           'USER': config('DB_USER'),
           'PASSWORD': config('DB_PASSWORD'),
           'HOST': config('DB_HOST'),
           'PORT': '',

# production
       'default': dj_database_url.config(

db_from_env = dj_database_url.config(conn_max_age=500)

ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=Csv())
Enter fullscreen mode Exit fullscreen mode

Deploying the app to Heroku

Create a postgres addon to your Heroku app

heroku addons:create heroku-postgresql:hobby-dev
Enter fullscreen mode Exit fullscreen mode

Next we log in to Heroku dashboard to access our app and configure it

Heroku dashboard
Add all your configurations in .env file directly to Heroku by running this command.

heroku config:set $(cat .env | sed '/^$/d; /#[[:print:]]*$/d')
Enter fullscreen mode Exit fullscreen mode

Remember to first set DEBUG to False and confirm that you have added all the configuration variables needed. Click on the Settings menu and then on the button Reveal Config Vars

Pushing to Heroku

Confirm that your app is running as expected before pushing. Runtime errors will cause deployment to fail so make sure you have no bugs and you have all the following; Procfile, requirements.txt with all required packages and runtime.txt.

Commit all the changes we have made and then:

git push heroku master
Enter fullscreen mode Exit fullscreen mode
  • If you are using main as the branch, change master to main

If you did everything correctly then the deployment should be done after a while with an output like this

Enumerating objects: 94, done.
Counting objects: 100% (94/94), done.
Delta compression using up to 8 threads.
Compressing objects: 100% (84/84), done.
Writing objects: 100% (94/94), 3.35 MiB | 630.00 KiB/s, done.
Total 94 (delta 24), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: -----> Python app detected
remote: -----> Installing python-3.6.6
remote: -----> Installing pip
remote: -----> Installing requirements with pip
remote:        Collecting config==0.3.9 (from -r /tmp/build_19aebf8f25d534a39e73b13219af9927/requirements.txt (line 1))
remote:          Downloading
remote:        Collecting dj-database-url==0.5.0 (from -r /tmp/build_19aebf8f25d534a39e73b13219af9927/requirements.txt (line 2))
remote:          Downloading
remote:        Collecting Django==1.11 (from -r /tmp/build_19aebf8f25d534a39e73b13219af9927/requirements.txt (line 3))
remote:          Downloading (6.9MB)
remote:        Collecting django-bootstrap3==10.0.1 (from -r /tmp/build_19aebf8f25d534a39e73b13219af9927/requirements.txt (line 4))
remote:          Downloading (40kB)
remote:        Collecting django-heroku==0.3.1 (from -r /tmp/build_19aebf8f25d534a39e73b13219af9927/requirements.txt (line 5))
remote:          Downloading
remote:        Collecting gunicorn==19.9.0 (from -r /tmp/build_19aebf8f25d534a39e73b13219af9927/requirements.txt (line 6))
remote:          Downloading (112kB)
remote:        Collecting Pillow==5.2.0 (from -r /tmp/build_19aebf8f25d534a39e73b13219af9927/requirements.txt (line 7))
remote:          Downloading (2.0MB)
remote:        Collecting psycopg2==2.7.5 (from -r /tmp/build_19aebf8f25d534a39e73b13219af9927/requirements.txt (line 8))
remote:          Downloading (2.7MB)
remote:        Collecting python-decouple==3.1 (from -r /tmp/build_19aebf8f25d534a39e73b13219af9927/requirements.txt (line 9))
remote:          Downloading
remote:        Collecting pytz==2018.5 (from -r /tmp/build_19aebf8f25d534a39e73b13219af9927/requirements.txt (line 10))
remote:          Downloading (510kB)
remote:        Collecting whitenoise==3.3.1 (from -r /tmp/build_19aebf8f25d534a39e73b13219af9927/requirements.txt (line 11))
remote:          Downloading
remote:        Installing collected packages: config, dj-database-url, pytz, Django, django-bootstrap3, whitenoise, psycopg2, django-heroku, gunicorn, Pillow, python-decouple
remote:          Running install for config: started
remote:            Running install for config: finished with status 'done'
remote:          Running install for django-bootstrap3: started
remote:            Running install for django-bootstrap3: finished with status 'done'
remote:          Running install for python-decouple: started
remote:            Running install for python-decouple: finished with status 'done'
remote:        Successfully installed Django-1.11 Pillow-5.2.0 config-0.3.9 dj-database-url-0.5.0 django-bootstrap3-10.0.1 django-heroku-0.3.1 gunicorn-19.9.0 psycopg2-2.7.5 python-decouple-3.1 pytz-2018.5 whitenoise-3.3.1
remote: -----> Discovering process types
remote: -----> Compressing...
remote:        Done: 56.3M
remote: -----> Launching...
remote:        Released v6
remote: deployed to Heroku
remote: Verifying deploy... done.
 * [new branch]      master -> master
Enter fullscreen mode Exit fullscreen mode

Run Migrations

heroku run python migrate
Enter fullscreen mode Exit fullscreen mode

If you wish to push your postgres database data to Heroku then run

heroku pg:reset
heroku pg:push <The name of the db in the local psql> DATABASE_URL --app <heroku-app>
Enter fullscreen mode Exit fullscreen mode

You can the open the app in your browse.

Last comment

Don't beat yourself up when you find it challenging to memorize language or library syntax. They have documentation for a reason, so feel free to reference it. The syntax will stick to your memory on frequent use.💯✍️


Top comments (0)