DEV Community

Cover image for How to Deploy a Django Application to Heroku
Biplov
Biplov

Posted on

How to Deploy a Django Application to Heroku

Introduction

Heroku is a cloud platform as a service(PaaS) that facilitates the deployment of web applications that supports several programming languages, including Python.
So let's look at how we can deploy a Django application to Heroku easily in no time.

Getting Started

The first thing you need to do is to create an account on heroku. You can create one from here. Once you have created an account you need to install the Heroku Command Line Interface (CLI) that will be used manage and scale your applications, view your application logs, and run your application locally. You also need to have git installed on your machine.

After the Heroku CLI is installed, open your terminal and log in to your heroku account using the following command:

$ heroku login

Managing the Application

Now CD into your project folder. We need to add a few files to be able to deploy the app in heroku. The first file you need is a requirements.txt file. If you have your virtual environment setup and active you can just type the following command on terminal to create your requirements file.

$ pip freeze > requirements.txt

Or you can manually list all dependencies on the requirements.txt file.

The next thing Heroku web applications require is a Procfile. Inside your project root folder, create a new file called Procfile and add the following content:

web: gunicorn myproject.wsgi

You need to change my-application with the name of your Django project.

Now you need to install something called Gunicorn. If you want to know more about Gunicorn you can follow this link. Use the following command to install Gunicorn:

$ pip install gunicorn

Add gunicorn to your requirements.txt file as well.

In addition you can also add a runtime.txt file to your app’s root directory that declares the exact version number to use. For example, you can create a new file runtime.txt and add the following content:

python-3.7.2

settings.py changes

Now you need to make some changes to your settings.py file. The first thing to do is install django-heroku. It is a package that automatically configures your Django application to work on Heroku.

$ pip install django-heroku

Be sure to add django-heroku to your requirements.txt file as well.

Now, add the following statement to the top of settings.py file:

import django_heroku

Then add the following to the bottom of settings.py:

# Activate Django-Heroku.
django_heroku.settings(locals())

Creating heroku app

Now we will create a new heroku app. Type the following command on your terminal:

$ heroku create my-app

Change my-app with your app name. It should be unique and not taken by anyone. Heroku will inform you if the name already exists.

This command will create your heroku app with the name you provided. Now you can visit your heroku dashboard on your browser to see your app. You can also see the default page of your application by using the link in the terminal. The link looks something like this:

https://my-app.herokuapp.com

Deploying the Django project

We will use git to deploy our project to heroku. CD to your project root folder and initialize the git using following command:

$ git init

You can add .gitignore file to your project. You can visit .gitignore.io to create .gitignore file for your project
Now add and commit the files to git:

$ git add --all
$ git commit -m 'initial commit'

Before you can push your project, you need to set your remote to heroku.

$ heroku git:remote -a my-app

Replace my-app with your app name.
Now, you can push your project to heroku:

$ git push heroku master

Then, migrate the database:

$ heroku run python manage.py migrate

Congratulations!! The deployment is complete. Your project is now live on heroku.

Conclusion

So, we deployed our project to heroku with not-so-good-looking domain name. Well, we can change that too. Heroku allows you to add a custom domain to your application. You can go through the Heroku Python docs for more information.

Top comments (0)