Django - Heroku

Host your Django website on Heroku

By Almin Piric | Talking Code | 28 Apr 2021


Deploying your website to Heroku can seem like a hassle. However if you follow this guide, you should be up and running in no time.
I will assume that you have a complete Django website that you are ready to publish. You will also need Git.

First we need to set up version control. It is important to initiate Git inside of the same folder as your manage.py file.
Enter the following commands into your Git Bash terminal and make sure to use your name and email address instead of the sample text.

git config --global user.name "YOUR NAME"
git config --global user.email "YOUR EMAIL ADDRESS"
git config --global push.default matching
git config --global alias.co checkout
git init

You will now see a hidden .git folder in your directory.
To add your project, you simply type git add .

The next step is to download the Heroku Toolbelt, which you can get here: https://devcenter.heroku.com/articles/heroku-cli

Once downloaded, restart your terminal. Navigate back to your main folder and turn on your virtual environment.

To make sure Heroku is installed correctly, type heroku -v
If you get a version number, you have installed it correctly.

Next, we need to create our SSH keys.
In your terminal, type mkdir ~/.ssh
We then need to go into this directory by typing cd ~/.ssh

To create our SSH key, we simply type ssh-keygen.exe
You will be asked where you want to save your key and whether you want to password protect it. We will simply hit enter for each prompt to save the file in the folder we have created and we will choose not to password protect the key. You will then have to hit enter a third time to confirm that you do not want to set a password.

You can make sure that your SSH key has been created by typing ls to show the contents of your .ssh folder.

Next, we will go back to our Django project. You should have the virtual environment running and be inside the main folder.

Now we need to create a Heroku account. You can do this here: https://www.heroku.com/
Go back to your terminal and type heroku login to connect to your Heroku account.

The next step is to add our SSH key to Heroku by typing heroku keys:add
You will see a prompt that says the key has been found and you need to confirm the upload by pressing y then enter.
Once the SSH key is uploaded, hit Ctrl + C to get back to your Django folder.

Next, we will create a Procfile that will tell Heroku how our website works. In the terminal, type touch Procfile
You will see the file in the same folder as your manage.py file.
Open the file and add the following command to set the webserver to gunicorn:
web: gunicorn DJANGO_PROJECT_NAME.wsgi
Make sure to replace DJANGO_PROJECT_NAME with the name of your project.

Now we need to use pip to install a few more things:

pip install django_heroku
pip install python-decouple
pip install dj_database_url
pip install gunicorn

You can use pip freeze to make sure that everything has been installed.

Next, we need to import everything into our settings.py file:

import django_heroku
from decouple import config
import dj_database_url

We also need to set debug mode to false and add heroku to allowed hosts:
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '.herokuapp.com']

Inside middleware, add the following AFTER Django security middleware:
'whitenoise.middleware.WhiteNoiseMiddleware',

Your middleware list should look like this:
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',
]

And finally add the following to the bottom of your settings.py file:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
django_heroku.settings(locals())

The next step is to create a requirements file for Heroku. This is done by typing pip freeze > requirements.txt in your terminal. This will create the requirements file and place it into your main folder.

Now we can push this commit to Heroku. Type the following into your terminal:
git add .
git commit -am "Initial commit"

Next, we will create the heroku app by typing heroku create
Once the app is created, you will be presented with a link to your Heroku hosted website.
You will most likely want to rename this app unless you have your own domain. To rename the app, simply type heroku rename YOUR_CUSTOM_NAME
The url will be YOUR_CUSTOM_NAME.herokuapp.com

We are now ready to push our code to Heroku by typing git push heroku master

Once complete, you should be able to visit your website by typing the url into your browser. If you get an Application error, you can see the issue by typing heroku logs into your terminal.

Congratulations! Your website is now live!

Big thanks to John Elder and Codemy. If you prefer video tutorials, you can see his great tutorial here 

If you would like to see the source code of Crypto Ledger, a Django website I host on Heroku, you can go to my GitHub: https://github.com/TMDStudios/crypto_ledger

You can see the website here: https://crypto-ledger.herokuapp.com/

How do you rate this article?


3

0

Almin Piric
Almin Piric

I am a freelance Python and Java developer, founder of TMD Studios, hobbyist game developer, and Bitcoin enthusiast. My work can be seen here: https://tmdstudios.wordpress.com/


Talking Code
Talking Code

A blog that focuses on open source projects with a touch of crypto and game-dev talk

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.