Back
Jun 14, 2017

How to Deploy a Django Application on Heroku?

Vladimir Kalyuzhny

1. Getting started on Heroku

First thing you need to do is to create an account. Heroku offers four pricing plans: Free, Hobby, Standard, Performance. In this tutorial we are going to use the Free-plan. It’s necessary to remember that this plan has certain limitations you need to peruse with before you deploy django app to heroku.

Creating an account on Heroku

If you already have an account on Heroku, skip this step. To create an account you need to fill out a registration form by clicking on the https://signup.heroku.com/identity link. It shouldn’t take you more than one minute neither bring any difficulties. The only thing I want to pay attention to is that don’t forget to choose Python at “Primary Development Language” line. After filling the form confirm your email and let’s get down to business.

Installing the Heroku CLI

We’ll need Heroku CLI. In this tutorial we will quite often use CLI to interact with Heroku.

After the installation process is finished, open console and enter the username and password of your account.

false

Creating Heroku App

false

Connecting PostgreSQL

false

2. Configuring django apps for heroku

For this article I wrote a simple Django application. First, we need to do some adjustments to our app before starting to deploy django to heroku.

requirements.txt

requirements.txt is a file which contains a list of all necessary app packages. Create this file in the root folder with such content inside:

false

Procfile

Procfile describes command need to be executed for app launch.

false

runtime.txt

It is the file in which we specify Python version

false

Separating Django settings

We need to get three files with the following settings:

  • settings/base.py - basic configuration
  • settings/production.py - production configuration (this particular file will be used on Heroku)
  • settings/local.py - local configuration

Such separation of settings will become necessary when our app starts to grow. You will find link to the code of these files at the end of this article.

Setting up static files

For the correct display of static files we need to modify settings.py и wsgi.py:

false
false

Database Configuration

In the previous step we’ve created PostgreSQL database. Let’s connect it to our app. Go to “Settings” and copy the entire DATABASE_URL contents.

After that, paste contents into settings/production.py., you must end up with this:

false

Local launch

Preliminary configuration of our app is done. Before we start to push django on heroku, let’s check how our app is displayed locally. For this we have heroku local web command.

false

If no mistakes were returned and you don’t see any of them in the console, then everything went well and you can proceed to the next step.

3. Deploying django to heroku

Logging into Heroku account

false

We'll push code and heroku should deploy the app automatically.

false

Running migrations

Don’t forget to add --settings=heroku_blog.settings.production, because that’s how system understands that particular file with production-server settings has to be used.

false

Checking the result

After completing all the steps above we get ready-to-go app which can be found at https://django-heroku-blog.herokuapp.com/

Logging

If you’re receiving errors while deploying an app, you can check what went wrong

false

4. Django Celery on Heroku

Redis installation on Heroku

false

Updating the requirements.txt file

Add two new packages:

false

Settings

Take REDIS_URL value from Settings and paste it to settings/base.py. After that, the Celery settings should look like that:

false

For Celery celery.py file has to be created in which celery initialization will take place. More details on how to set up celery in Django you can find here Using Celery with Django.

false

Let’s add celery test-task. This task was taken from the documentation and what it simply does is it returns a sum of two numbers.

false

Updating Procfile

As we have seen before, Procfile contains commands that need to be executed if we want to launch an app. We need to launch celery at the moment of the app launch. To do this we need to update procfile by adding the string worker: celery worker -A heroku_blog -E -l info. As a result, procfile should look like that:

false

Deploying Сelery on Heroku

false

Testing

We’re all set! Now let’s double-check if we did all correctly:

Open console and run a command

false

In the next tab run log output, there should appear statement that the celery task was completed successfully.

false

That’s it. The app’s source code you can find here: https://github.com/barbossa/django-heroku-blog.

More thoughts