How to deploy django app on heroku for free

·

3 min read

Over the past few days, I have tried deploying a number of simple Django apps on heroku and every time, I used to end up getting errors and could not deploy it in one go. I looked up for the tutorials and blog posts on web but most of them were a bit complicated for my basic projects. I had to look for the help from stackoverflow and github posts to solve issues while deploying.

So after doing some research and trying it out, I found that those are the steps I need to follow if I want to deploy my basic Django app ( without separate static files example) on heroku and I just want to document those steps in this post.

I am using the windows machine, so this might be a help for anyone using the same. This post will only show how to deploy the app built with Django and if we don't have one, we might need to create one.

This post will list only the steps while keeping the explanations for later. Here are the steps:

Prerequisites:

We need Python and pip installed on the system. We also need to install git (link). Make a copy of your project in a new folder if you don't want to mess up with the project files for deployment. Make sure to activate the virtual environment. 1/ We need to install following packages

$ pip install gunicorn
$ pip install whitenoise
$ pip install dj-database-url
$ pip install psycopg2

2/ Add all the dependencies that we installed in an environment to 'requirements.txt' file, create a 'Procfile' and 'runtime.txt', all inside the root directory (in the same directory where we have manage.py file). To do so,

i. Adding to requirements file

$ pip freeze > requirements.txt

ii. Create a file called 'Procfile' and inside this file paste the following:

web: gunicorn your_project.wsgi —log-file -

Note that you enter your_project name . You need to use the name of your project there.

iii. Create 'runtime.txt' file and define the Python and version

python-3.8.1

Check the supported version in devcenter.heroku.com

3/ In the 'settings.py',

Add the following in MIDDLEWARE list:

MIDDLEWARE = [
  # 'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',

]

Database update for heroku (inside the same settings file)

import dj_database_url

db_from_env = dj_database_url.config(conn_max_age=600)
DATABASES['default'].update(db_from_env)

4/ We need a heroku account, if you don't have an account create it here (link).

We also need to download Heroku CLI (link)

5/ Login to heroku using

$ heroku login

6/ Create a heroku app by typing the command:

$ heroku create your_app_name

7/ Now inside 'setting.py', add your heroku app in ALLOWED_HOST list and change DEBUG to False

DEBUG = False
ALLOWED_HOSTS = ['your_app_name.herokuapp.com', 'localhost', '127.0.0.1']

8/ We don't want heroku to run collectstatic on our behalf for now, so we can disable the collectstatic build step by typing the following command:

$ heroku config:set DISABLE_COLLECTSTATIC=1

9/ Type the following commands to add files to git.

$ git init
$ git add .
$ git commit -m "First commit"

10/ Push the files to remote heroku repository:

$ git push heroku master

11/ Open the app in heroku using

$ heroku open

Later on if we want to change the code and push it deploy it again, use following commands after making an edit:

$ git add .
$ git commit -m "edit"
$ git push heroku master