{{announcement.body}}
{{announcement.title}}

How to Deploy a Django Web Application to Heroku

DZone 's Guide to

How to Deploy a Django Web Application to Heroku

In this article, we discuss how to deploy a Django web application to Heroku in order to find a cost-effective source for hosting for your project.

· Web Dev Zone ·
Free Resource

Introduction

Django is a high-level Python web Framework use it to build scalable web applications, while Heroku is a cloud application platform that facilitates the deployment of web applications.

Heroku supports several programming languages, including Python.

Install the Heroku Toolkit

To install the Heroku Toolkit, create an account on Heroku. Then, install the Heroku Toolbelt. It's a very powerful CLI that allows users to manage their applications.

After installing the Toolbelt, open your Terminal and login to your account:

Shell
xxxxxxxxxx
1
 
1
$ heroku login
2
 
          
3
Enter your Heroku credentials.
4
 
          
5
Email: your email
6
 
          
7
Password (typing will be hidden):
8
 
          
9
Authentication successful.


Preparing our Application

CD into your application folder.

Shell




xxxxxxxxxx
1


 
1
$ cd my-application/



Here is the list of things you will need to add to your project:

  • Add a Procfile in the project root.
  • Add a requirements.txt file with all the requirements in the project root.
  • Add Gunicorn to the requirements.txt file.
  • Add a runtime.txt to specify the correct Python version in the project root.
  • Configure whitenoise to serve static files.
You may also like: Best Web Hosting Services to Use for Testing Your Code.

The Procfile

Create a file named Procfile in the project root. Add the following content:

Shell
xxxxxxxxxx
1
 
1
web: gunicorn my-application.wsgi --log-file -


Note: change my-application with the name of your Django project.

The Requirements.txt File

Run this command, this command will list all dependencies :

Shell




xxxxxxxxxx
1


1
$ pip freeze > requirements.txt



You should see something like this:

Plain Text




x
13


1
dj-database-url==version
2
 
          
3
Django==version
4
 
          
5
gunicorn==version
6
 
          
7
psycopg2==version
8
 
          
9
psycopg2-binary==version
10
 
          
11
pytz==version
12
 
          
13
whitenoise==version



The Runtime.txt File

Create a file named runtime.txt in the project root, and put the specific Python version your project uses. For example, this example project will use Python 3.8

Set Up the Static Assets

Configure the STATIC-related parameters in settings.py:

Python
xxxxxxxxxx
1
13
 
1
# Static files (CSS, JavaScript, Images)
2
 
          
3
# https://docs.djangoproject.com/en/2.2/howto/static-files/
4
 
          
5
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
6
 
          
7
STATIC_URL = '/static/'
8
 
          
9
STATICFILES_DIRS = [
10
 
          
11
    os.path.join(BASE_DIR, "static")
12
 
          
13
]


Configure Whitenoise

Install Whitenoise

Shell
xxxxxxxxxx
1
 
1
$ pip install whitenoise


Add **Whitenoise** to your Django application in the **wsgi.py** file:

Python
xxxxxxxxxx
1
23
 
1
"""
2
 
          
3
WSGI config for repertoire project.
4
 
          
5
It exposes the WSGI callable as a module-level variable named ``application``.
6
 
          
7
For more information on this file, see
8
 
          
9
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
10
 
          
11
"""
12
 
          
13
import os
14
 
          
15
from whitenoise.django import DjangoWhiteNoise
16
 
          
17
from django.core.wsgi import get_wsgi_application
18
 
          
19
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my-application.settings')
20
 
          
21
application = get_wsgi_application()
22
 
          
23
application = DjangoWhiteNoise(application)


Update the **settings.py** file.

Python
xxxxxxxxxx
1
 
1
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'


Add **whitenoise middleware** at the top of the middleware list in **settings.py**.

Deploy the Application

Update Database Configuration in settings.py (at the bottom of the file).

Python
xxxxxxxxxx
1
 
1
import dj_database_url 
2
 
          
3
prod_db  =  dj_database_url.config(conn_max_age=500)
4
 
          
5
DATABASES['default'].update(prod_db)


Create the App in Heroku From Terminal

Python
xxxxxxxxxx
1
 
1
$ heroku create myapplication
2
 
          
3
Creating  myapplication... done
4
 
          
5
https://myapplication.herokuapp.com/ | https://git.heroku.com/myapplication.git


Choose any name for your app. Heroku will inform you if the name already exists.

Add Your App Domain Name to ALLOWED_HOSTS in Settings.py

Python
xxxxxxxxxx
1
 
1
ALLOWED_HOSTS = ['myapplication.herokuapp.com']


Initialize Git and connect your new app (or existing one) to Heroku Git remote repository and push the application.

In your terminal:

Shell
xxxxxxxxxx
1
 
1
$ git init
2
 
          
3
$ heroku git:remote -a myapplication
4
 
          
5
$ git add .
6
 
          
7
$ commit -m "Initial commit"
8
 
          
9
$ git push heroku master


If you get an error message with collectstatic, simply disable it by instructing Heroku to ignore running the manage.py collecstatic command during the deployment process.

Shell
xxxxxxxxxx
1
 
1
$ heroku config:set     DISABLE_COLLECTSTATIC=1  


Then, run:

Shell
xxxxxxxxxx
1
 
1
$ git push heroku master


Migrate the database :

Shell
xxxxxxxxxx
1
 
1
$ heroku run python manage.py migrate


Let me know in the comment section if you have an error. Thank you for reading.


Further Reading

Topics:
django, heroku, python, tutorial, web dev

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}