How to deploy a Django application for Heroku with Git CLI

Tram Ho


Heroku is a cloud platform hosting service provider. It supports several programming languages ​​including PHP, Node.js and Python. It is Platform-as-a-Service (PaaS) that allows you to manage website applications while it takes care of your servers, networks, storage and other cloud components.

In this article, we will look at how to deploy a Django application for Heroku, using Git.

You can follow the same steps and deploy the application from GitHub, if it is hosted there.


Here’s a list of things to do before we start rolling out:

  • Git
  • Heroku and CLI accounts
  • Django Apps Heroku Command Line Interface (CLI) makes it easy to create and manage your Heroku applications directly from your terminal. It’s an essential part of using Heroku.

To install Heroku CLI (also known as Heroku Toolbelt), please follow the instructions on the official website.

Make sure that your Django application is running in a virtual environment that you need to keep running throughout your deployment.

Heroku account

Once all of these are installed, the next step is to create a free Heroku account here, if you don’t already have one.

After writing the following into a terminal:

The terminal will display a message like:

Press any key and log in with your browser. The terminal then displays a message along the lines:

Configure the Django application for Heroku

Now that we’ve finished the prerequisites, let’s get the Django app for Heroku ready.


A Procfile is a file named Procfile that does not need any file extensions placed in the root directory of your application. It lists the types of processes in an application, and each process type is a declaration of a command to be executed when a container / dyno of that type of process is started.

Before creating Procfile, you will want to install the django gunicorin your project directory:

Although Django exports Procfilex with its own WSGI server, we will ask Heroku to use Gunicorn to service our application, which is also recommended by Heroku.

Now, let’s create a Procfilet in the parent directory and add the following line:

Replace yourdjangoweb with the actual name of your project.


Create a text file called runtime.tx in the same directory as Procfile. It tells Heroku what version of Python your app is using. If you are unsure about the version, enter python –version into the terminal with your Django virtual environment enabled.

Then add the version to runtime.txt:

Servers are allowed

This is an additional security measure in Django to prevent HTTP server header attacks by only allowing sites served at the server / domain to be added to the ALLOWED_HOSTS list. If Debug = Truev and ALLOWED_HOSTSis [] then only localhost is enabled by default. To deploy and serve your web application on Heroku, add it to the list:

Alternatively, you can allow all apps from Heroku by omitting the app name as follows:

You can also use [‘*’] to enable all servers. You can find more details in the documentation.

Install the package

Below are the additional packages that need to be installed in our virtual environment.

To connect our Django database to Heroku, install dj-database-url by calling:

Since Heroku uses the Postgres database, we also need its adapter for Python:

And finally, we will use WhiteNoise to serve static files in the production server. We can install by calling:

We have already configured gunicorn, let’s add the configuration for whitenoisand dj-database-url.

static file configuration

Let’s first add the basic settings for serving static files in any Django project. You can copy-paste the following into :

Add WhiteNoise to MIDDLEWARE the list right after SecurityMiddleware that will be at the top:


Database configuration

We are using dj-database-url to configure our database. Add these lines at the bottom of :


Heroku will only recognize a deployed application as a Python application if it has a requirements.txttfile in the root directory. It tells Heroku what packages are needed to run your application.

We can use pip freezev and convert the output to requirements.txttfile for this:

Your requirements.txt should include the following:

Note: The versions above are used by our Django app, and they may differ for you.

Connect the Heroku app to Git

Now we need to create a Heroku app:

It will display a “done” message with two URLs along this line:

This means a Git repository has been created in the Heroku cloud for your app. The URL will be used to access your app anywhere but we still need to take one more step before running our app, i.e. we need to push your code into the archive.

Initialize an empty repository in your project directory:

Connect your Heroku app to free gitkho storage:

Add files to the staging area:

Commit changes / files:

Finally, push the project to a gitrepo hosted on Heroku:

If all goes well, you should see an output like this:


Share the news now

Source : Viblo