How to deploy Django on Heroku

This page explains how to deploy a Django application on Heroku, the popular deployment platform.


Prerequisites

  • Basic programming knowledge in Python
  • Basic Django knowledge and WSGI concept
  • Comfortable using a terminal
  • Already familiar with GIT


What is Django


Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

django Links


What is Heroku


Heroku’s a fully-managed platform that helps developers to deploy apps with ease. Heroku is a cloud-based, fully-managed platform as a service (Paas) for building, running, and managing apps.

A short-list with features:

  • Runtime - Heroku empowers developers to deliver products using a CLI, called Heroku Toolbelt
  • PostgreSQL DBMS - a powerful database already configured to be production-ready
  • Automatic scaling - Heroku scales in an instant, both vertically and horizontally.
  • Github integration - trigger production builds directly from Github commits

Heroku Links


To explain the process, we will use a simple Django Boilerplate already enhanced for a Heroku deployment.

Sample project - Django Boilerplate


Django Boilerplate is a template codebase used by the AppSeed platform to generate Django Web Apps enhanced with a basic set of features:

  • UI-Ready, Django Native templating
  • SQLite database, Native Django ORM
  • Session-Based auth flow (login, register)

As mentioned, the project comes pre-configured for Heroku. The relevant files are listed below:

  • runtime.txt - specify the Python version used by Heroku during the build and deploy
  • Procfile - configuration file that informs Heroku where to look for the WSGI interface
  • requirements.txt
  • settings.py file - to add the required modules provided by Heroku platform


Requirements.txt update


To have a successful deployment on Heroku, the usual requirements.txt file should be updated to contain some new modules:

  • gunicorn - the Gunicorn server to start our app
  • django-heroku - a helper bundle provided by the Heroku development team to make the deployment much easier.


File - runtime.txt


To build the deploy any python-based app, Heroku uses a default Python version python-3.6.10 or the one specified in the runtime.txt file. Supported environment, as per Heroku official documentation - Specifying a Python version:

  • python-3.8.3
  • python-3.7.7
  • python-3.6.10 <– The Default Version
  • python-2.7.18


Procfile


Heroku apps include a Procfile that specifies the commands that are executed by the app on startup. As specified in the official docs, the Procfile is always a simple text file that is named Procfile without a file extension.

web: gunicorn core.wsgi --log-file=-

For our sample, gunicorn is called with core.wsgi argument.


Update settings.py

The main app settings file should be updated to include django-heroku as below:

# File: core/settings.py

# Usual import here ..
...

import django_heroku     # <-- Include this

...
# Common Django settings
...

# Add this code at the end of the file
# Activate Django-Heroku.
django_heroku.settings(locals())


How to deploy on Heroku

  • Create a FREE account on Heroku platform
  • Install the Heroku CLI that match your OS: Mac, Unix or Windows
  • Open a terminal window and authenticate via heroku login command
  • Clone the sources and push the project for LIVE deployment

The full command list executed on a sample project provided by the AppSeed platform.

$ # Get the code
$ git clone https://github.com/app-generator/boilerplate-code-django.git
$ cd boilerplate-code-django
$
$ # Heroku Login
$ heroku login
$
$ # Create the app in Heroku platform
$ heroku create # a random name will be generated by Heroku
$
$ # Disable collect static
$ heroku config:set DISABLE_COLLECTSTATIC=1
$
$ # Push the source code and trigger the deploy
$ git push heroku master
$
$ # Execute DBSchema Migration
$ heroku run python manage.py makemigrations
$ heroku run python manage.py migrate
$
$ # Visit the deployed app in browser.
$ heroku open
$
$ # Create a superuser
$ heroku run python manage.py createsuperuser

At this point, you should be able to visit the app in the browser.


Help & Resources