Latest stories

Python : Deploy Django application on production using Heroku

January 24, 2020

Deploy Django web application to Heroku


Heroku is a cloud application platform, it facilitate the deployement of a web application. They support several programming languages, include Python.

Install Heroku Toolbet

To install heroku Toolket Sign up to heroku, Then install the Heroku Toolbet it is very powerful command line tool it will help you to manage your application. After installing the Toolbet, open your Terminal/CMD and login to your account :

$ heroku login
Enter your Heroku credentials.
Email: your email
Password (typing will be hidden):
Authentication successful.

Preparing our application

place into your application

$ cd my-application/

Here is the list of things you will probably need to add to your project:

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

The Procfile

  • Create a file named Procfile in the project root
  • Add the following content : web: gunicorn my-application.wsgi --log-file -Note: change my-applicatio with the name of your Django project.

The requirements.txt

Run this command, this command will list all dependencies :

$ pip freeze > requirements.txt
You should see somthing like this :

The runtime.txt

Create a file named runtime.txt in the project root, and put the specific Python version your project use: python 3.8

Set Up The Static Assets

Configure the STATIC-related parameters on

# Static files (CSS, JavaScript, Images)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
    os.path.join(BASE_DIR, "static")

Configure Whitenoise

Install Whitenoise

$ pip install whitenoise

Add the Whitenoise to your Django application in the file:

WSGI config for repertoire project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
import os
from whitenoise.django import DjangoWhiteNoise
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my-application.settings')
application = get_wsgi_application()
application = DjangoWhiteNoise(application)

Update the

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

Add whitenoise middleware at the top of the middleware list in

Deploy the application

Update Database Configuration in (at the bottom of the file)

import dj_database_url 
prod_db  =  dj_database_url.config(conn_max_age=500)

Create App in Heroku from terminal

$ heroku create myapplication
Creating ⬢ myapplication... done |

Choose any name for your app. Heroku will inform you if the name already exists

Add your app domain name to ALLOWED_HOSTS in


Initialize Git and connect your new app (or existing one) to Heroku Git remote repository and push the application. in your terminal / CMD :

$ git init
$ heroku git:remote -a myapplication
$ git add .
$ commit -m "Initial commit"
$ git push heroku master

If you get an error message with collectstatic, simply disable it by instructing Heroku to ignore running the collecstatic command during the deployment process.

$ heroku config:set     DISABLE_COLLECTSTATIC=1  

Then, run :

$ git push heroku master

Migrate the database :

$ heroku run python migrate

Let me know in comment section if you’ve any error.

Thank you for reading.

Ousseynou Diop

Written by Ousseynou Diop who lives and works in Dakar, Senegal building useful things.You should follow him on Twitter