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

$ 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.
  • This is useful if you have multiple applications on your computer that use different versions of certain software packages( python 2.7 for application 1 and python 3.6.4 for application 2)

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.
  • Use ctrl+c to close the server

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)


$ 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



$ 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……’


$ 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
  • These can be placed inside a new folder named settings
  • In addition, create an empty file named so that the folder can be recognized as a python module.
  • Modify the DJANGO_SETTINGS_MODULE in the file to
  • Inside your heroku dashboard, add a variable called DJANGO_SETTINGS_MODULE and set it to
  • Now you can overide settungs like DEBUG = False inside your and DEBUG=True inside your contains all settings shared by both the development and the production environment.

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

Glorified CRUD practitioner. Lets connect on twitter @NdamuleloNemakh