Deploy a Django App on Heroku – An Easy Step-by-Step Guide

Deploy A Django App On Heroku

Hello folks! In this tutorial, we are going to discuss how we can deploy our Django app on Heroku, free of cost. So, let’s get started with the exciting journey of deploying our first Django app on the Heroku platform that also at zero cost.

If you do not know Django, we have a Django tutorial series that you can follow through.

What is Heroku?

Heroku is a platform on the cloud which enables developers across all over the world to develop or build, run, and operate various kinds of web applications. It comes under PaaS (Platform As A Service) which is one of the widely used and most popular cloud computing service offerings.

Heroku fully supports students’ learning that’s why apart from its paid features it also offers free services for easy experimentation and deployment. One can easily work with programming languages like Java, Node.js, Scala, Clojure, Python, PHP, and Go on the Heroku platform as it supports all of these.

Why to Deploy our Django app on Heroku?

Whenever we learn any programming language or framework like Django in Python, we do all sorts of development works on our local computer which is good enough to learn and debug things. But after we have finished our development work and our project is ready to go and to be used by some real-world users, it becomes necessary to deploy the project or the application on some web servers.

So that it becomes accessible to all the potential users. Most importantly, it creates a very deep and positive impact regarding our development work because it is live on the internet and people can easily see the things working in real-time.

Steps to Deploy a Django App on Heroku

Following are the five key steps that are involved in the deployment process of a Django app on Heroku.

1. Create a Django app which you wish to deploy on Heroku

If you have already created and developed your Django project (a web app or website), that’s great. You can skip this step. For those who don’t have a Django project now available with them but still want to learn the deployment process of a Django app on Heroku. You can run the following commands to create a new Django project and app.

> python -m pip install Django
> django-admin startproject <your_project_name>
> python manage.py migrate
> python manage.py runserver

Output:

Django version 3.2.6, using settings '<your_project_name>.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

NOTE: Do not forget to try running the Django app on your local server (http://127.0.0.1:8000/) before moving ahead with its deployment process.

2. Create a repository for the Django app on GitHub

Log in to your GitHub account and created a brand new repo (repository) to store all the folders, files, and code of your Django project. Also, make your current Django project’s directory a git repo and connect it to the remote GitHub repository. Then stage everything, commit, and finally push everything to the remote GitHub repo.

3. Make the following changes to the Django project files

  • Create a new file with the filename Procfile in the Django project’s directory and copy the following code to it.
web: gunicorn <your_project_name>.wsgi --log-file -
  • Install the following dependencies from the command line interface or from the virtual environment if any.
> python -m pip install gunicorn
> python -m pip install whitenoise
  • Modify the settings.py file inside the project’s sub folder to add the allowed hosts and set the DEBUG parameter to False in the following ways.
DEBUG = False

ALLOWED_HOSTS = ['127.0.0.1', '<site_name>.herokuapp.com']
  • Modify the settings.py file again to update the MIDDLEWARE code with the whitenoise dependency in the following ways.
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
     # Added Following One Line Of Code
    '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',
]
  • Update the settings.py file inside the project’s sub folder in the following ways which is required for the smooth working of media and static files.
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("home.urls")),
    # Added Following Two Lines Of Code
    url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}), 
    url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}), 
]
  • Create a requirements.txt file which will tell the server about the various dependencies of the Django project and their versions required for the smooth deployment process and working of the Django app using the following command.
> python -m pip freeze > requirements.txt

NOTE: Do not forget to stage, commit, and then push the modifications or changes to the remote (GitHub repository).

4. Create a free account on the Heroku platform

Go to www.heroku.com and create a free Heroku account just by providing the following mandatory details.

  • First name
  • Last name
  • Email address
  • Role
  • Primary development language

If you have already had a Heroku account, no need to create a new one simply login to it on your default web browser.

5. Create and setup a new Heroku app on the Heroku dashboard

Following are the steps to create and set up a new Heroku app.

  • Go to the Heroku Dashboard and click on the New button.
  • Select the Create New App option from the drop down.
  • Choose an appropriate App name available for use.
  • Go the App Settings pannel and select Python in the Buildpacks section.
  • Switch to the App Deploy pannel and connect your GitHub account in the Deployment method section.
  • Search for the GitHub repo containing the Django project and select it.
  • Select the git branch usually a master/main under the Manual deploy section and click the Deploy Branch button.

Hurrah! You have successfully launched your Django App or Website on the Heroku server.

Summing-up

In this tutorial, we have learned about the Heroku platform, what is the need for deployment, steps to deploy a Django App or Website on the Heroku platform. Hope you understood the deployment process and are excited to deploy your Django App or Website on Heroku. Thanks for reading! Stay tuned with us for more amazing learning content on Python stuff.