Photo by Anna Shvets from Pexels

Ultimate 2020 Django on Heroku deploy guide

Matej Meglic
5 min readNov 10, 2020

--

A step-by-step guide on how to quickly prototype and showcase your Django apps with a free-tier Heroku package

Prologue:
I don’t really consider myself a developer, but a mere enthusiast. I found Django earlier in 2020 and used it in a couple of prototype projects that never saw the light of day. I saw the potential of a front-end/back-end combo I loved the possibility of moving past vanillaJS/React+Netlify pages that I was creating. I lacked backend, database and more versatility and Django provided.

I found it very non-trivial to deploy to Heroku and I didn’t find any good step-by-step beginners tutorials and spent multiple hours debugging errors and trying to make up my mind on Stack overflow.

As I was struggling with these twice, following a great youtube tutorial, I decided to “hack” my way into making publicly-available notes on the matter or how to quickly set up projects so that it can be exhibited for initial feedback with no upfront maintenance costs.

Disclaimer: this was written and works as described in November 2020. Some likely mistakes or steps can or should be optimized — I concatenated some of the code and didn’t split it into obvious functionality steps as this is meant to be hacky prototyping, not explaining what every action, bit, and piece of code does.

The final project as deployed is available at GitHub, but it will take more than c/p to make it run on Heroku.

Assumptions:

  1. GitHub account
  2. GitHub CLI installed
  3. GitHub CLI login process completed
  4. Heroku account
  5. Heroku CLI installed
  6. Heroku CLI login process completed
  7. Node.js/npm installed
  8. Code editor installed (in my case VS Code)
  9. Python installed
  10. Django installed
  11. pipenv installed

Pre-process (make this proper):

Your environment is all set, now you need to create a Django app. I extended the app past the point of initial index.html to include a model so that I was able to simulate a database migration and admin panel as well.

1. create a new git repository on GitHub
2. django-admin startproject djangoheroku ← that is my project name
3. cd ./REPONAME ← jump in the folder
4. pipenv shell ← installs pipenv virtual environment in your project
5. pipenv install Django
6. python manage.py startapp polls ←that is my app name
7. git init
8. git add --all
9. git commit -m “Initial commit”
10. git remote add origin GITHUB CLONE LINK
11. git push -u origin master
12. python manage.py runserver ← initiate localhost
13. Check http://127.0.0.1:8000/

Install PEP8 linter/prettier
pipenv install flake8 — dev
pipenv install black — dev — pre

At this point, you should code your app and design it to your liking… For this guide, I took an original tutorial Write your first Django app steps 1 and 2 as my starting point, and after completing Step 2, you should [in theory] be ready to do the next steps.

The app works locally

[terminal]
python manage.py makemigrations polls
python manage.py migrate

Hint: Don’t worry about the database as on the localhost, as we are utilizing default SQLite database on the local environment and we will migrate to Heroku Postgres database — meaning, you will have to re-do all the data you might have already saved locally.

Ready, Set … Deploy to Heroku

You had finished with development on the local environment and the code runs smoothly. Now off to production, exposing the newest masterpiece to the public.

Install dependencies

[terminal]
pipenv install gunicorn
pipenv install django-heroku
pipenv install python-dotenv

Create files

.gitignore
- on the 1st level of the project
- copy/paste contents of this gist

Procfile
- on the 1st level of the project (no .txt!)
- web: gunicorn djangoheroku.wsgidjangoheroku should be the name of the file that contains the settings.py file

.env
- in the same file that holds the settings.py file
- store secrets in that file
SECRET_KEY=”5ds3f2bfghf5n4g…”generate your own GUID
DEBUG_VALUE=”True”

Create a Heroku app

[terminal]
heroku login
heroku create APPNAME (in my case djangoheroku4711)
heroku open

Initial Heroku app works

heroku config:set SECRET_KEY=”bz6fttk75…”
heroku config:set DEBUG_VALUE=”True”

Settings.py

  1. add code on top of the file

import django_heroku
from dotenv import load_dotenv
load_dotenv()

2. change parameters (replace lines):

SECRET_KEY = os.getenv(“SECRET_KEY”)
DEBUG = os.getenv(“DEBUG_VALUE”) == “True”
ALLOWED_HOSTS = ["djangoheroku4711.herokuapp.com"]

3. add code to the bottom of the file
STATIC_ROOT = os.path.join(BASE_DIR, ‘staticfiles’)
SECURE_PROXY_SSL_HEADER = (“HTTP_X_FORWARDED_PROTO”, “https”)
SECURE_SSL_REDIRECT = True
django_heroku.settings(locals())

Push to Git

[terminal]
git add — all
git commit -m “Initial commit”
git push heroku master

Configure Postgres database

[terminal]
heroku run bash
python manage.py migrate
python manage.py createsuperuser
exit

Test your application — It should work now

https://djangoheroku4711.herokuapp.com/polls/ https://djangoheroku4711.herokuapp.com/admin

Don’t forget to set the debug variable to False on Heroku

[terminal]
heroku config:set DEBUG_VALUE=”False”

Final project folder structure

I hope this guide helped you in your quest to migrate the Django app from your local machine to Heroku. thus making it publicly available. Please comment on the flow so that I can further improve it.

--

--