Configuring Django apps to deploy to heroku - HashedIn Technologies

Configuring Django apps to deploy to heroku

Technology - 09 Jun 2017
Devashish Sharma

This blog is a part of our blog series on Django development. We have built a Django application named Charcha, and now lets see the required steps to deploy this application to Heroku.

Install Heroku CLI

Sign up for an account on Heroku, then follow this link to install Heroku CLI.

Once the CLI is setup, login from your terminal using the following command:

Configuring the application for Heroku

Heroku web applications require a Procfile. This file is important to declare application process types and entry points. It must be located in the root of the repository. Here is the content of the Procfile that we will use:

This requires Gunicorn, the production web server for Django applications.

Installing gunicorn

Database Configuration

For this we use the dj-database-url library to extract database configurations from the environment.

For Django applications, a Heroku Postgres hobby-dev database is automatically provisioned. This populates the DATABASE_URL environment variable.

To setup the database, we will add the following code in settings.py:

Static assets management and serving

Configure the static directories in settings.py:

By default, Django does not serve static files in production. We will use WhiteNoise for serving static assets in production.

Installing Whitenoise

settings.py

wsgi.py

Building the app locally

Run the following command to test heroku deployment locally.

This will run the Procfile and you can debug any errors if any on your local machine.

Deploy to Heroku

We are now ready to deploy our application. Follow these steps to successfully deploy to heroku.

Commit all changes to git.

Login to Heroku

Create an app Lets name the app “charcha”. Heroku will create a random appname if none is provided.

Push to heroku remote

After the app is deployed, use this command to open the app in your browser:

Summary

We have learned how to configure our application to deploy easily on Heroku. We learnt about dj-database-urlto setup database and whitenoise to serve static files in production.

Share
Tweet
+1
Share

E-book on Digital Business Transformation