Deploying Django App on Heroku

Srivastava
5 min readDec 13, 2020

Made an awesome app with Django!!! Excited to show your awesome work to the world. Let’s do it together 😉.

Deploying Django Project On Heroku
Deployment Django to Heroku

What is Heroku ?

Heroku is a container-based cloud Platform as a Service (PaaS). It is used to deploy, manage, and scale modern apps.

It is basically a cloud application platform , that supports several programming languages, and allows developers to host there app globally . It relies on AWS and is trusted by thousands of companies.

This article will explain you all the necessary steps to push in production a Django app, using Heroku for hosting .

So, let’s begin…

First, you need to create an create an account on HEROKU. You can create your free account here : HEROKU .

Installing Required Tools…

In this article we will be deploying our django app using Heroku CLI(Command Line Interface), as it makes it easy to create and manage our Heroku apps directly from the terminal.

You can download Heroku CLI from here , as Heroku CLI requires GIT , which can be downloaded from here .

You can check whether your Heroku CLI and GIT are properly installed , by running the following commands in your shell or cmd :

For HEROKU CLI :

heroku login

For GIT :

git --version

Open your cmd or shell whatever you use, and navigate to your project folder . I recommend you to make a copy of your project folders and delete all the unnecessary files or folders.

Now lets first create a virtual environment just outside your app folder or main folder, you can do it by installing pipenv

pip install pipenv

Its basically something that will isolate your project. So anything that you will be installing in your computer will not effect your project.

Now activate your virutal environment by running the following commands,

for Windows run :

virtualenv .
.\Scripts\activate

for Shell run:

virtualenv envsource env/bin/activate

Now navigate to the folder where your ‘manage.py’ file exists . And try running your Django app using *python manage.py runserver* command. It will probably wont work and give you some errors and mainly the Django error 😳.

Which means you need to install the modules with exact the same version that you have installed for making your project. Installing same version is necessary, as different versions may give errors.

How to install the same modules versions?

Open your folder in your ide and in ide terminal write

pip freeze

This command will help you know what all libraries or modules you had in you project folder along with their versions, copy and install all the necessary modules that you are using in your project, in you cmd or shell using ‘PIP’ command.

After successfully installing all your modules, try running your django project using ‘runserver’ command, And this time it should not throw you an error. Close your running server from cmd using “CTRL+C”.

Adding necessary files…

  • runtime.txt

Make a new runtime text-file in your manage.py folder and write the python version you are using .For example

python-3.9.1
  • Procfile

Make a new file named ‘Procfilewithout any extension. So, it is recommended to make it available in your manage.py folder using your ide. It is a file which is required by HEROKU for deploying the apps. Procfile is a mechanism for declaring what commands are run by your application’s dynos on the Heroku platform.

What is gunicorn?

Gunicorn (Green Unicorn) is a pure-Python HTTP server for WSGI applications. It allows you to run any Python application concurrently by running multiple Python processes within a single dyno. It provides a perfect balance of performance, flexibility, and configuration simplicity.

In simple word , it’s basically a WSGI(Web Server Gateway Interface ) that is used for running the Python web application.

Installing gunicorn in your virtual environment:

pip install gunicorn

Write the following commands inside your procfile

web: gunicorn your_app_name.wsgi --log-file -

Install django-heroku

The django-heroku package automatically configures your Django application to work on Heroku.

It basically make the work of publishing django project very easy.

pip install django-heroku

Now, add some changes in you ‘settings.py’ file, at the top add

import django_heroku

simultaneously, at the bottom add

django_heroku.settings(locals())

If you want to add allowed hosts to your django app ,you can add but dont forget to make

DEBUG=False
  • requirements.txt

Now write freeze command in your cmd or shell, and it will give you a list of packages that are required on the HEROKU server. So to make it available on the server write,

pip freeze > requirements.txt

This command will basically make a text-file with all the required packages. It contains all the necessary files that is needed by your application to be installed on the server. If you add any new packages for your app in future you need to add them in this file, either manually typing into it or by running the above given command again.

Creating Heroku app

You can create your app either from Heroku-dashboard or by using your CLI . Here we will be using our cmd or shell .

Hope you are still logged in your heroku dashbord through cmd or shell ,if not use “heroku login” command to get logged.

heroku create your_app_name 

this command will create an heroku app for you. Make sure your app name is unique.

Adding files on server using git.

Run the following commands on your cmd or shell,

git init
git status
git add --all
git commit -m “anything of your choice"

Creating a HEROKU remote master:

heroku git:remote -a your_app_name

pushing to heroku master

git push heroku master

Setting up heroku admin panel

Go to admin panel

your_app_name.herokuapp.com\admin

try logging in, and it will probably not work 😶 .

Let’s resolve it🤟

heroku run bashpython manage.py migratepython manage.py createsuperuser

Type in the details asked, and your superuser will be created. And this will make your admin panel available to you.

Congratulations!!!🎉🎊You have finally deployed your Django App on Heroku server🍺🍾.

Though it’s bit longer but will definitely help you to deploy you Django app on HEROKU server. If this deployment walk through was really helpful to you, then share your reviews in the comment and don’t forget to give it a clap 👏.

--

--