Back
Jun 14, 2017

How to Deploy a Django Application on Heroku?

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.

$ heroku login
Enter your Heroku credentials:
Email: django-heroku-tutorial@gmail.com
Password: **
Logged in as django-heroku-tutorial@gmail.com

Creating Heroku App

$ heroku create django-heroku-blog

Creating ⬢ django-heroku-blog... done

https://django-heroku-blog.herokuapp.com/ | https://git.heroku.com/django-heroku-blog.git

Connecting PostgreSQL

$ heroku addons:create heroku-postgresql:hobby-dev

Creating heroku-postgresql:hobby-dev on ⬢ django-heroku-blog... free
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pg:copy
Created postgresql-acute-85745 as DATABASE_URL
Use heroku addons:docs heroku-postgresql to view documentation

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:

dj-database-url==0.4.2
Django==1.11.1
gunicorn==19.7.1
psycopg2==2.7.1
whitenoise==3.3.0

Procfile

Procfile describes command need to be executed for app launch.

web: gunicorn heroku_blog.wsgi --log-file -

runtime.txt

It is the file in which we specify Python version

python-3.6.1

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:

# settings/base.py
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
STATIC_URL = '/static/'

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
# wsgi.py
import os

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "heroku_blog.settings.production")

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

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:

# settings/production.py
import dj_database_url

from .base import *


ENVIRONMENT = 'production'
DEBUG = False
ALLOWED_HOSTS = ['']
DATABASES['default'] = dj_database_url.config(
    default='DATABASE_URL_HERE'
)

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.

$ heroku local web

12:44:01 web.1   |  [2017-05-12 12:44:01 +0300] [31988] [INFO] Starting gunicorn 19.7.1
12:44:01 web.1   |  [2017-05-12 12:44:01 +0300] [31988] [INFO] Listening at: http://0.0.0.0:5000 (31988)
12:44:01 web.1   |  [2017-05-12 12:44:01 +0300] [31988] [INFO] Using worker: sync
12:44:01 web.1   |  [2017-05-12 12:44:01 +0300] [31992] [INFO] Booting worker with pid: 31992

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

heroku login

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

$ git push heroku master

Object count: 63, done.
Delta compression using up to 4 threads.
Compression: 100% (55/55), done.
Write: 100% (63/63), 294.76 KiB | 0 bytes/s, done.
Total 63 (delta 9), reused 4 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing python-3.6.1
remote: -----> Installing pip
remote: -----> Installing requirements with pip
remote:        Collecting Django===1.11.1 (from -r /tmp/build_075c2ff1315d45c636d7ab6ff1d6d586/requirements.txt (line 1))
remote:          Downloading Django-1.11.1-py2.py3-none-any.whl (6.9MB)
remote:        Collecting psycopg2==2.7.1 (from -r /tmp/build_075c2ff1315d45c636d7ab6ff1d6d586/requirements.txt (line 2))
remote:          Downloading psycopg2-2.7.1-cp36-cp36m-manylinux1_x86_64.whl (2.7MB)
remote:        Collecting gunicorn==19.7.1 (from -r /tmp/build_075c2ff1315d45c636d7ab6ff1d6d586/requirements.txt (line 3))
remote:          Downloading gunicorn-19.7.1-py2.py3-none-any.whl (111kB)
remote:        Collecting whitenoise==3.3.0 (from -r /tmp/build_075c2ff1315d45c636d7ab6ff1d6d586/requirements.txt (line 4))
remote:          Downloading whitenoise-3.3.0-py2.py3-none-any.whl
remote:        Collecting dj-database-url==0.4.2 (from -r /tmp/build_075c2ff1315d45c636d7ab6ff1d6d586/requirements.txt (line 5))
remote:          Downloading dj_database_url-0.4.2-py2.py3-none-any.whl
remote:        Collecting python-decouple==3.0 (from -r /tmp/build_075c2ff1315d45c636d7ab6ff1d6d586/requirements.txt (line 6))
remote:          Downloading python-decouple-3.0.tar.gz
remote:        Collecting pytz (from Django===1.11.1->-r /tmp/build_075c2ff1315d45c636d7ab6ff1d6d586/requirements.txt (line 1))
remote:          Downloading pytz-2017.2-py2.py3-none-any.whl (484kB)
remote:        Installing collected packages: pytz, Django, psycopg2, gunicorn, whitenoise, dj-database-url, python-decouple
remote:          Running setup.py install for python-decouple: started
remote:            Running setup.py install for python-decouple: finished with status 'done'
remote:        Successfully installed Django-1.11.1 dj-database-url-0.4.2 gunicorn-19.7.1 psycopg2-2.7.1 python-decouple-3.0 pytz-2017.2 whitenoise-3.3.0
remote:
remote: -----> $ python manage.py collectstatic --noinput
remote:        77 static files copied to '/tmp/build_075c2ff1315d45c636d7ab6ff1d6d586/assets', 103 post-processed.
remote:
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 67M
remote: -----> Launching...
remote:        Released v4
remote:        https://django-heroku-blog.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy.... done.
To https://git.heroku.com/django-heroku-blog.git
 * [new branch]      master -> master

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.

$ heroku run python manage.py migrate --settings=heroku_blog.settings.production

Running python manage.py migrate --settings=heroku_blog.settings.production on ⬢ django-heroku-blog... up, run.6570 (Free)
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying sessions.0001_initial... OK

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

$ heroku logs --tail

2017-05-12T12:25:15.000000+00:00 app[api]: Build started by user django-heroku-tutorial@gmail.com
2017-05-12T12:25:38.517361+00:00 app[api]: Release v8 created by user django-heroku-tutorial@gmail.com
2017-05-12T12:25:38.517361+00:00 app[api]: Deploy a90e5e3 by user django-heroku-tutorial@gmail.com
2017-05-12T12:25:38.888015+00:00 heroku[web.1]: Restarting
2017-05-12T12:25:38.888721+00:00 heroku[web.1]: State changed from up to starting
2017-05-12T12:25:15.000000+00:00 app[api]: Build succeeded
2017-05-12T12:25:39.760955+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2017-05-12T12:25:39.778876+00:00 app[web.1]: [2017-05-12 12:25:39 +0000] [9] [INFO] Worker exiting (pid: 9)
2017-05-12T12:25:39.782260+00:00 app[web.1]: [2017-05-12 12:25:39 +0000] [4] [INFO] Handling signal: term
2017-05-12T12:25:39.791042+00:00 app[web.1]: [2017-05-12 12:25:39 +0000] [8] [INFO] Worker exiting (pid: 8)
2017-05-12T12:25:39.983593+00:00 app[web.1]: [2017-05-12 12:25:39 +0000] [4] [INFO] Shutting down: Master
2017-05-12T12:25:40.276436+00:00 heroku[web.1]: Process exited with status 0

4. Django Celery on Heroku

Redis installation on Heroku

$ heroku addons:create heroku-redis:hobby-dev

Creating heroku-redis:hobby-dev on ⬢ django-heroku-blog... free
Your add-on should be available in a few minutes.
! WARNING: Data stored in hobby plans on Heroku Redis are not persisted.
redis-rectangular-25956 is being created in the background. The app will restart when complete...
Use heroku addons:info redis-rectangular-25956 to check creation progress
Use heroku addons:docs heroku-redis to view documentation

Updating the requirements.txt file

Add two new packages:

celery==4.0.2
redis==2.10.5

Settings

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

# settings/base.py
CELERY_BROKER_URL = 'REDIS_URL_HERE'
CELERY_RESULT_BACKEND = 'REDIS_URL_HERE'

CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT = ['json']

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.

# celery.py
from future import absolute_import, unicode_literals
import os

from celery import Celery


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'heroku_blog.settings.base')
app = Celery('heroku_blog')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

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.

# blog/tasks.py
from celery import shared_task


@shared_task
def add(x, y):
    return x + y

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:

web: gunicorn heroku_blog.wsgi --log-file -
worker: celery worker -A heroku_blog -E -l debug

Deploying Сelery on Heroku

$ git push heroku master

Testing

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

Open console and run a command

$ heroku run python manage.py shell  --settings=heroku_blog.settings.production
from  blog.tasks import add
add.delay(7, 9)

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

$ heroku logs --tail

2017-05-12T12:25:21.601945+00:00 app[worker.1]: [2017-05-12 12:25:21,600: INFO/MainProcess] Received task: blog.tasks.add[ae5ffbd6-a307-4b13-97e8-b50aabd2cc66]
2017-05-12T12:25:22.601934+00:00 app[worker.1]: [2017-05-12 12:25:22,614: INFO/PoolWorker-1] Task blog.tasks.add[ae5ffbd6-a307-4b13-97e8-b50aabd2cc66] succeeded in 0.009866348467767239s: 16

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

Subscribe for the news and updates

More thoughts
Sep 1, 2021TechnologyBusiness
Top 10 Web Development Frameworks in 2021 - 2022

We have reviewed the top web frameworks for server and client-side development and compared their pros and cons. Find out which one can be a great fit for your next project.

May 10, 2018Technology
How to Build a Cloud-Based Leads Management System for Universities

Lead management is an important part of the marketing strategy of every company of any size. Besides automating various business processes, privately-held organizations should consider implementing an IT solution that would help them manage their leads. So, how should you make a web-based leads management system for a University in order to significantly increase sales?

Mar 12, 2017Technology
Creating a chat with Django Channels

Nowadays, when every second large company has developed its own instant messenger, in the era of iMessages, Slack, Hipchat, Messager, Google Allo, Zulip and others, I will tell you how to keep up with the trend and write your own chat, using django-channels 0.17.3, django 1.10.x, python 3.5.x.

Dec 11, 2016Technology
Auto WebSocket Reconnection with RxJS (with Example)

In this RxJS tutorial article, we will focus on restoring the websocket connection when using RxJS library.

Sep 23, 2010Technology
Dynamic class generation, QuerySetManager and use_for_related_fields

It appears that not everyone knows that in python you can create classes dynamically without metaclasses. I'll show an example of how to do it.So we've learned how to use custom QuerySet to chain requests:Article.objects.old().public()Now we need to make it work for related objects:user.articles.old().public()This is done using use_for_related_fields, but it needs a little trick.

Jul 1, 2010Technology
Overriding QuerySet in Django

As you know, model managers can be overriden in Django. It's convenient to add custom filtration method.