Configuring Django Apps To Deploy To Heroku

Devashish Sharma

09 Jun 2017

This article is part of our blog series on Django development, where we explore ways to deploy Django apps to Heroku. We have built a Django application named Charcha, and now let us 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 set up, log in from your terminal using the following command:

$ heroku login
Enter your Heroku credentials.
Email: devashish@hashedin.com
Password (typing will be hidden):
Authentication successful.

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:

web: gunicorn charcha.wsgi

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

Installing gunicorn

$ pip install gunicorn
$ pip freeze > requirements.txt

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 set up the database, we will add the following code in settings.py:

# Update database configuration with $DATABASE_URL.
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)

Static assets management and serving

Configure the static directories in settings.py:

# https://docs.djangoproject.com/en/1.9/howto/static-files/
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

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

 

Installing Whitenoise

$ pip install whitenoise
$ pip freeze > requirements.txt

settings.py

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

wsgi.py

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
application = DjangoWhiteNoise(application)

Building the app locally

Run the following command to test Heroku deployment locally.

$ heroku local web

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

Deploy to Heroku

Finally, we are now ready to deploy our application. Follow these steps to successfully deploy to Heroku.
Commit all changes to git.

$ git add .
$ git commit -m "Added a Procfile."

 

Login to Heroku

$ heroku login
Enter your Heroku credentials.
...

 

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

$ heroku create charcha

 

Push to Heroku remote

$ git push heroku master

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

$ heroku open

Summary

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


Have a question?

Need Technology advice?

Connect

+1 669 253 9011

contact@hashedin.com

facebook twitter linkedIn youtube