In order to test a Django application, a best practice is to have a sandbox of a Django project to run the tests in it. Since we have a functioning project into the repository (cf. How-To: enhance the Python + Django compatibility of a Django application), why not reusing it to show a demo? That is what we have done for the Django TailorDev Biblio project and we tell you how we deployed it to Heroku in the sequel.
Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud. It takes care of most of the annoying things related to software deployment.
In order to deploy the sandbox to Heroku, you will have to create two
(Heroku-) specific files (
runtime.txt) at the root of the
$ echo 'web: gunicorn sandbox.wsgi --log-file -' > Procfile $ echo 'python-3.6.1' > runtime.txt
runtime.txt file tells Heroku which Python version must be used for the
project while the
Procfile states how to run it. In our case, we use the
gunicorn WSGI server.
requirements.txt file must include all the required dependencies
to run on Heroku. It includes common Django dependencies such as
psycopg2) as well as less common
whitenoise, which will be described later.
# requirements.txt # WSGI gunicorn # Database psycopg2 # Settings dj-database-url # Statics whitenoise
You will also have to adapt your sandbox settings as follows:
As you may have noticed, we configure two unusual dependencies in this
- whitenoise: a wonderful application used to serve static files;
dj-database-url: a small
utility to configure your database-related settings with an URL that can be
found in a
DATABASE_URLenvironment variable (supplied by Heroku).
The project is now properly configured to run on Heroku (and also other PaaS by the way).
You will need to login to your Heroku account (or create an account first). Once logged in, create a new application and connect it to your GitHub project:
Configuration variables are environment variables forwarded to your application. You will have to create two variables via the Heroku dashboard:
DJANGO_SECRET_KEY: this will be your instance secret key (see the Django documentation for further information);
IS_PUBLIC_SANDBOX: set this value to
1or anything you want as long as it exists, it will inactivate the
DEBUGmode (cf. Settings).
Note that the
DATABASE_URL variable is automatically created for Django
applications pushed to Heroku \o/
Perform database migrations
Install the Heroku Command Line Interface (one of the best CLI I have ever used ), and then login to perform database migrations:
$ heroku login $ heroku run -a my_app python sandbox/manage.py migrate
In this example, we assume that the application is registered as
Heroku. And voilà! Your app should be up and running at:
You will find the demo of our Django TailorDev Biblio project at: tailordev-biblio.herokuapp.com. It might take a while since Heroku “lazy-starts” applications running on free plans, which is perfectly fine for demo