Codementor Events

The easiest way to deploy Django application

Published Dec 11, 2019
The easiest way to deploy Django application

Deploy Django web application to Heroku

Introduction

Heroku is a cloud application platform, it facilitate the deployement of a web application.
They support several programming languages, include Python.

Install Heroku Toolbet

To install heroku Toolket Sign up to heroku,
Then install the Heroku Toolbet it is very powerful command line tool it will help you to manage your application.
After installing the Toolbet, open your Terminal/CMD and login to your account :

$ heroku login
Enter your Heroku credentials.
Email: your email
Password (typing will be hidden):
Authentication successful.

Preparing our application

place into your application

$ cd my-application/

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

  • Add a Procfile in the project root;
  • Add requirements.txt file with all the requirements in the project root;
  • Add Gunicorn to requirements.txt;
  • A runtime.txt to specify the correct Python version in the project root;
  • Configure whitenoise to serve static files.

The Procfile

  • Create a file named Procfile in the project root
  • Add the following content :
    web: gunicorn my-application.wsgi --log-file -
    Note: change my-applicatio with the name of your Django project.

The requirements.txt

Run this command, this command will list all dependencies :

$ pip freeze > requirements.txt
You should see somthing like this :
dj-database-url==version
Django==version
gunicorn==version
psycopg2==version
psycopg2-binary==version
pytz==version
whitenoise==version

The runtime.txt

Create a file named runtime.txt in the project root, and put the specific Python version your project use:
python 3.8

Set Up The Static Assets

Configure the STATIC-related parameters on settings.py:


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static")
]

Configure Whitenoise

Install Whitenoise

$ pip install whitenoise

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

"""
WSGI config for repertoire project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
"""
import os
from whitenoise.django import DjangoWhiteNoise
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my-application.settings')
application = get_wsgi_application()
application = DjangoWhiteNoise(application)

Update the settings.py

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)

import dj_database_url 
prod_db  =  dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(prod_db)

Create App in Heroku from terminal

$ heroku create myapplication
Creating ⬢ myapplication... done
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.

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 / CMD :

$ git init
$ heroku git:remote -a myapplication
$ git add .
$ commit -m "Initial commit"
$ 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.

$ heroku config:set     DISABLE_COLLECTSTATIC=1  

Then, run :

$ git push heroku master

Migrate the database :

$ heroku run python manage.py migrate

Let me know in comment section if you've any error.

Thank you for reading.

Discover and read more posts from Ousseynou Diop
get started
post comments2Replies
Sarvesh Gandhi
5 years ago

Hi,

Please mention that there is a “-” between “python” and its version in runtime.txt file. Heroku says it is case-sensitive. It will save time.

Also, I was expecting toggling DEBUG in settings.py, but it is working after making changes with WhiteNoise.
The WhiteNoise site says, Breaking changes.
The latest version of WhiteNoise removes some options which were deprecated in the previous major release.

Thanks

Ousseynou Diop
5 years ago

Thank you for the feedback -:)