Hey, Web Developers 👨‍💻!

While you were working on web development, you must have come across certain issues. So, in this article, we’ll be covering those troublesome issues and figuring out how to deploy Django on Heroku.

Some issues appear during your web development task so here I will be covering up a very frustrating problem which is How To Deploy Django On Heroku.

Before we leap into the guide of deployment of Django on Heroku, let’s understand the need for deploying Django on Heroku.

By deploying a Django application using Heroku in python, The developers can use Django from anywhere using the free service of Heroku.

Deploying Django on Heroku can be quite a stressful task with the prohibition of static files, bugs, and glitches that you may wanna or may not wanna encounter.

Just stick to the article and we’ll figure out the easiest way possible to Deploy Django On Heroku.

Let’s start with What Is Django?

Django is the most popular open-source framework in the web development industry. 

It is a high-level python web framework specially built to boost up the development of secure and maintainable websites. Built by experienced developers. 

Free Django Hosting

Django follows the MTV(model-template-view) architecture pattern, it takes care of the Hassle of web development issues, so that you don’t have to be worried about the foundation of the code. As it has pre-installed functions and classes, You have to maintain your focus on writing your app.

Do you know What Is Heroku?

Heroku is a cloud platform service that supports several famous programming languages.

Heroku is one of the most known Cloud platforms also known as the PaaS(platform as a service). It supports various programming languages including python and totally used by developers to deploy, run and scale applications in the simplest path to upload their apps in the market. Heroku also allows developers to deploy any 5 web applications under the size of 500 MB.

How to Deploy django on heroku: Step-by-step Guide

Okay! This was just the intro part of Django and Heroku and keeping time in mind we don’t need to dig into the details. 
Now, let’s go through the process of deploying a python app and Django on Heroku, Also Check Django deployment on shared hosting.

What We are going to Learn in this Tutorial?

  • Expected files for Python
  • Install Heroku Toolbelt
  • Python deployment
  • Python versions and upgrades
  • Preparing the Application 
  • The  procfile
  • The requirements.txt
  • The runtime.txt
  • Setup the static Assets
  • Deployment
  • Wrap Up

Expected files for Python

  • Requirements.txt
  • Setup.py
  • Pipfile 

If any of these three files are present in Heroku’s root directory. It will automatically identify your app as a python app.

If not then the python buildpack will fail to identify your application correctly.

How To Install Heroku Toolbelt

  • Open an account on Heroku 
  • Download and install Heroku Toolbelt from https://toolbelt.heroku.com/
  • To check whether it was installed correctly by running Heroku on cmd. 

Run the below command on your cmd.

$heroku version

If cmd prints the version of Heroku that means it is installed.

  • Then run the login command that will directly terminate you to Heroku.
$heroku login
  • Now login and you are ready to use Heroku’s service

Python deployment flow

When you deploy your python app to Heroku, the dependencies which you specify in your requirements.txt file automatically install before the app starts up.

If you are using Django’s collectstatic command which runs automatically during the deployment procedure. This command can be quite tricky to be configured properly. To make it a bit easier, add the Django-Heroku Python package which sets up everything.

To automatically perform other tasks before your app is deployed, you can add a release phase command to your app.

Python versions and upgrades

Generally, newly created python apps work on python-3.9.5 runtime and your app basically works on the version which you have used when it was first deployed.

You can specify a Python runtime, by adding a runtime.txt file to your app’s root directory that declares the exact version number to use.

If the app python version is not updated then you will be notified in your app’s build output.

Preparing the Application

So, The list of things which you’ll probably need for preparing the application project are:

  • Add a Procfile in the project
  • Save requirements.txt file with all the requirements in the project root data
  • Add Gunicorn to requirements.txt
  • A runtime.txt to specify the correct Python version in the project root
  • Configure white noise to serve static files.

The Procfile

Procfile explicitly declares that what command should be executed to start your app.

In the project root create a file named Proctfile and add the following code:

web: gunicorn <project_name>.wsgi --log-file -

The Requirements.txt

Create the main requirements.txt file in the root directory and Simply add this line of code:

pip freeze > requirements.txt

pip freeze command is used to show all the install files.

Whether your python application is deployed or not on Heroku. It will depend on the requirements.txt file. If it has a requirements.txt file in its root directory then Heroku will indicate the deployment of your python application.

Your requirements.txt file must be like this:


The runtime.txt

Again, in the root directory add the runtime.txt file and specify the correct python version for your web app by adding the following line to your application


Set Up The Static Assets

Now, Open setting.py file and make these changes:

  • Set DEBUG = False
  • Modify ALLOWED HOST=[′, ‘.herokuapp.com’]
  • Allow whitenoise to take over while disabling Django’s static file by adding nostatic to the top of your
"INSTALLED_APP" list.INSTALLED_APPS = [ 'whitenoise.runserver_nostatic','django.contrib.staticfiles', # …
  • Add whitenoise at the top of the middleware list in setting.py
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', # ...]
  • Update Database settings
  • Set
  • Add this to setting.py
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'


The Heroku deployment process is done by using Git. Where your app will be stored in a remote git repository in the Heroku cloud.

Why do we need a Git repository?

Well, it acts as an update to your deployed app. Git repository even helps to restore app files when a new update is crashed or has too many annoying bugs. .git/ repository tracks down the changes made to a file in your project while maintaining the project history.

Alright, then it is time for the deployment.

  • First and foremost clone the repository you want to deploy using below command:
  • git clone https://github.com/vitorfs/yourproject.git && cd your project
  • Login to Heroku Toolbelt
Heroku login
  • Inside the project root, create a Heroku app
heroku create demo-yourproject

Choose any name for your app. If the name already exists then Heroku will choose a name for you.


Creating ⬢ demo yourproject… donehttp://demo yourproject.herokuapp.com/ | https://git.heroku.com/demo yourproject.git

  • Add your app domain name to ALLOWED_HOSTS in settings.py.
ALLOWED_HOSTS = ['herokudjangoapp.herokuapp.com']
  • Initialize Git and connect your new app (or existing one) to Heroku Git remote repository.
  • Add a PostgreSQL database to your app:
Heroku addons:create Heroku-PostgreSQL:hobby-dev
Output : Creating PostgreSQL-metric-21979... done, (free)Adding PostgreSQL-metric-21979 to demo-Bootcamp... doneSetting DATABASE_URL and restarting demo-Bootcamp... done, v4The database has been created and is available
  • Now click on Heroku dashboard and access your recently created app.
  • Then, Click on the Settings menu and then on the button Reveal Config Vars:
  • Now, we need to add all the environment variables. 

So, open setting.py and make these changes.

SECRET_KEY = config('SECRET_KEY')DEBUG = config('DEBUG', default=False, cast=bool)DATABASES = { 'default': dj_database_url.config( default=config('DATABASE_URL') )}
  • Push to deploy

git push Heroku master

OutputCounting objects: 1999, done.Delta compression using up to 4 threads.Compressing objects: 100% (616/616), done.Writing objects: 100% (1999/1999), 461.28 KiB | 0 bytes/s, done.Total 1999 (delta 1139), reused 1983 (delta 1131)remote: Compressing source files... done.remote: Building source:remote:remote: -----> Python app detectedremote: -----> Installing python-2.7.12remote: $ pip install -r requirements.txt
remote: -----> Launching...remote: Released v5remote: https://demo-bootcamp.herokuapp.com/ deployed to Herokuremote:remote: Verifying deploy.... done.To https://git.heroku.com/demo-bootcamp.git * [new branch] master -> master
  • Migrate the database

Heroku run python manage.py migrate

Output:Running python manage.py migrate on ⬢ demo-Bootcamp... up, run.9352Operations to perform: Apply all migrations: questions, sessions, authentication, articles, feeds, contenttypes, messenger, activities, authRunning migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying questions.0001_initial... OK Applying feeds.0001_initial... OK Applying articles.0001_initial... OK Applying activities.0001_initial... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying authentication.0001_initial... OK Applying messenger.0001_initial... OK Applying sessions.0001_initial... OK

Now you’re good to go!

To check Visit yourproject.herokuapp.com 

How to Deploy Django on Heroku: Step-by-step Guide


So, this was all about this guide on Deploying Python and Django Apps on Heroku, hope you liked it. One more thing Heroku is very expensive and if you want to go with its alternatives then you can choose from the google app engine, open shift.
But always keep this thing in mind that cheaper services come with many limitations and Heroku is a bunch of features.

Best Django Hosting for Students
How to Generate a QR Code in Django