Deploy your Django app on Heroku without using Heroku CLI (easiest way)

Auroshis Ray
4 min readFeb 9, 2021

This blog will help you to deploy you Django application on Heroku without using Heroku CLI as mentioned in 7 easy steps.

while writing this article I assume you are able to create and run your own Django app in localhost, have basic knowledge of git and have a GitHub account.

Step 1 : Install django-heroku, dj-database-url gunicorn whitenoise libraries with the given commands

pip install django-heroku
pip install dj-database-url gunicorn whitenoise

Step 2 : Add the below line at the bottom in the settings.py file in your main project directory

django_heroku.settings(locals())

Step 3 : Add ‘whitenoise.middleware.WhiteNoiseMiddleware’ in the middleware list in settings.py as the second entry as shown below

MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ]

Step 4 : Configure your database connection, I have used PostgreSQL

DATABASES = {'default': {'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': ''
,'PORT': ''
,'USER': ''
,'PASSWORD': ''
,'HOST': '',}}

All the above fields left as empty strings are found in Heroku which I will show in the steps.

Step 5 : add the following imports to settings.py at top, set DEBUG = False, add .herokuapp.com to allowed hosts as shown below

import dj_database_url
import django_heroku
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1', '.herokuapp.com', 'localhost']

Step 6 : setup an app in Heroku & database connection credentials

click on create new app
Give a name to your app, select region.

It’s not mandatory to add to pipeline. Click on create app.

you will be redirected to this dashboard

Go to Settings -> Buildpacks as shown below

Adding Python buildpack
In resources add Heroku Postgres

A pop up will appear as Submit Order form, click on that.

After adding Heroku Postgres it will be in your resources list

Click on the above item. It will take you the database dashboard. Then click on settings -> view credentials.

Database credentials

Sorry for poor marking out, it looks ugly. Now, copy these credentials to their respective fields in database section in settings.py as mentioned in step 4.

After adding new database connection make sure you run the migrations again using

python manage.py migrate

Step 6 : Add the following lines for static files in settings.py

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')STATIC_URL = '/static/'STATICFILES_DIRS = [os.path.join(BASE_DIR, 'core/static')]STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

STATICFILES_DIRS contains the path where you have stored your static files. In my case it is ‘core/static’.

Step 7 : Create Procfile, requirements.txt, runtime.txt

create a file named as Procfile without any extensions in the same directory which contains manage.py file. Other two mentioned files will also be created in the same directory.

Inside Procfile add the following line

web: gunicorn <name of your project>.wsgi - log-file -

Name of your project is that project name which is given by you while creating your project using the django-admin startproject <name of your project> command.

Inside runtime.txt add the following line

python-3.9.1

Basically it defines the version of Python your app will be running on.

requirements.txt will be autogenerated by the following command.

pip freeze > requirements.txt

It may appear overwhelming for the first time but once you do this, it’s easy to do it again. Now the fun part starts where we will deploy the app.

Push your code to a branch in a GitHub repository

Go to your Heroku app dashboard shown in step 6 and then to Deploy section

Deploy section

Click on connect to GitHub and do the formalities. Connect to your desired repository. It will reflect in App connected to GitHub.

You may use Enable Automatic Deploys. It’s an awesome option where you just have to push your modified code to the right branch in your repository and it will reflect after few moments in your deployed project. It’s like dynamic deployment. But sometimes there are restrictions on how many times you can deploy your code.

I prefer manual deployment. Select the branch you want to deploy and click on Deploy Branch.

You will get a link to your deployed project like : errormonger.herokuapp.com

The app link provided above is the link to my personal project which helps software developers to reduce wastage of time in searching for redundant errors and track the kind of errors a developer is committing.

Hurray🎊, You have successfully deployed your Django project.

--

--

Auroshis Ray

Web developer, engineer, cricket fan, love to build