Deploy Django to Heroku Using gunicorn + postgresql + whitenoise + virtualenv

In this tutorial , we are going to show how to deploy your Django 2.0 web application on the Heroku platform.

This tutorial assumes that you are familiar with the django framework, git and the Linux command line. In addition if you haven’t done so already, register an account with Heroku.

Django dependencies SETUP

  1. Install virtualenv on your linux machine

$ sudo apt-get install virtual env


$ sudo pip install virtualenv

If you dont have pip installed in your system, install it using

$ python3 -m pip install — user — upgrade pip

  • A virtual environment such as (virtual env) allows you to run your application in an isolated environment from your local machine.

2. Navigate to your working directory to Create and Activate the virtual environment

$ virtualenv .vEnv

$ . .vEnv/bin/activate

  • Take care not to omit the dots!

3. Install django 2.0 using pip( Note: You need to have python3 and pip pre-installed on your local machine)

$ pip install django==2.0

4. In your current working directory, start a new django project called coffee

$ django-admin startproject coffee .

$ python3 runserver

  • The second command will run a local development server on your local machine.Navigate to localhost:8000 on your web browser to see if your project was created successfully.

5. Next, let us install all dependencies required for our django application to run on heroku.

$ pip install gunicorn

$ pip install python-decouple

$ pip install whitenoise

$ pip freeze > requirements.txt

  • Pip freeze allows you to store all your dependencies in one file( i.e. requirements.txt)


  1. Initialize git and commit all changes we’ve made so far

$ git add .

$ git commit -m “Initial commit”

2. Login to heroku

$ heroku login

3. Create a heroku application (Note: Your app name must be unique, if endeesa is not available try something else!)

$ heroku create endeesa

4. Add heroku postgres database add-on to your new application

$ heroku addons:create heroku-postgresql:hobby-dev

5. Modify our file to look like this

# Copy your secret key to a new file called .env (See step number 8)

import os
from decouple import config
import dj_database_url



# SECURITY WARNING: don’t run with debug turned on in production!
DEBUG = config(‘DEBUG’, default=False, cast=bool)


# Recommended setting is [ ‘’ ]


‘default’: dj_database_url.config(


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

6. Your file inside the project folder should look like this:

import os

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “coffee.settings”)
# Remember to change coffe.settings to your_project_name.settings

application = get_wsgi_application()
application = DjangoWhiteNoise(application

7. Create a new file in the root directory and name it Procfile (No extension)

web: gunicorn coffee.wsgi — log-file -

# Coffee is the name of your django project — You can change it as required!

8. Create a new file and name it .env (no extension). Add the following contents:

SECRET_KEY=’copy-your-secret-key-in-yout-settings .py file before modifying it’

DATABASE_URL= # No quotes

9. Create a runtime.txt file and add the following


10. Update your requirements.txt file so that it looks like this



  1. After you finish ALL the steps listed above, Deploy your application to heroku as follows

$ heroku config:set DISABLE_COLLECTSTATIC=1

$ git add .

$ git commit -m “your commit message”

$ git push heroku master

2. Open the application in your browser by running

$ heroku open

$ heroku ps:scale web=1

3. Copy the DATABSE_URL value from heroku app dashboard to your .env file

On your web browser:

Go to your application dashboard > settings > Reveal config vars > DATABASE_URL

Inside your .env file add:

DATABASE_URL: ‘pxcyyyyss……’


  1. Now to continue editing your site locally run the following:

$ pip install -r requirements.txt

$ heroku local

2. Change ALLOWED_HOSTS=[‘*’] inside and check if the application is running on localhost:5000 or on localhost:8000 your web browser

3. Now you can continue developing your django application locally. Remember to disable the DEBUG variable inside once your site is ready for final deployment.

  • The best practice adopted by many developers is to modify the django project structure such that you have three settings files (e.g. , and

Example commands on the local environment:

$ python startapp

$ python makemigrations

4. Update the remote site as follows

$ git add .

$ git commit -m “Remote site update example”

$ git push heroku master

Written by

Former Glorified Electrician(aka Electrical Engineer). Now a Software Developer working on complex Enterprise Software. Lets connect on twitter @NdamuleloNemakh

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store