Deploying a Django Project on Heroku 🚀
Tags: django, python, heroku, tutorial
Published: 2022-02-05 22:17:17

So congrats 🎉 you built your Django project and want to deploy it on Heroku 🚀. In this tutorial we are gonna go through the necessary steps to deploying a django project on Heroku, So let's start.

## Install Libraries

First let's start by installing a couple of necessary libraries.

**1. Install django_heroku**
Install [django_heroku](https://pypi.org/project/django-heroku/) library using the command below. It's a Django library for Heroku applications that ensures a seamless deployment and development experience.

```
pip3 install django-heroku
```

_Note that django_heroku uses Heroku's default PostgresDB so you don't have to change anything in your `settings.py ` DATABASES variable if you want to use Postgres as your database, otherwise you have to configure your own DB and provide your credentials in the DATABASES variable._


**2. Install gunicorn**

Install [Gunicorn](https://pypi.org/project/gunicorn/) a Python WSGI HTTP Server for UNIX.

```
pip3 install gunicorn
```

## Required Files

Then let's prepare the necessary files before deployment.

**1. requirements.txt**

You need to save all the packages you've installed and used in your project in your `requirements.txt` file using the following command.

```
pip3 freeze > requirements.txt
```

**2. Procfile**

Create your `Procfile` it doesn't have any extension just type the file name like mentioned above and add these couple of lines to it.
```
release: export DEVELOPMENT=True
release: python manage.py makemigrations --no-input
release: python manage.py migrate --no-input

web: gunicorn [YOUR_PROJECT_NAME].wsgi
```
This file is responsible for running any necessary commands that you wish to execute before starting the server. Feel free to edit the file but don't remove the last line which is responsible for starting your server.

**3. runtime.txt**
Add this file and include the Python version that your project is currently using.

```
python-3.8.0
```

**4. Edit settings.py**
Finally we need to add a couple of lines to your `settings.py` file

First Import the django_heroku library
```python
import django_heroku
```

Then add this line to the end of your `settings.py` file
```python
django_heroku.settings(locals())
```
## Publish to GitHub

Create a repo on GitHub and push your project. 
>_Like you haven't done that already 👀😅_

## Create Heroku App

Create an account on Heroku if you don't have one and create a new app. Enter your app name as well as your preferred region.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0jk8ek8u7vdc29bwd9r.png)

## Connect Heroku Project to Github Repo

Navigate to the **deploy** tab inside your app dashboard and select GitHub as your **deployment method**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpzls5iuwdkjbhy2rs30.png)

Search for your repo name and click on **Connect**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g285wpb0n1fc7pomkder.png)

## Enable Automatic Deployments

Under **Automatic deploys** select the branch you wish to deploy 🚀 whenever new commits are pushed on that branch it gets deployed automatically or if you don't wish to do that you can just select the branch you want to deploy under **Manual deploy** and then select Deploy Branch. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e739yqnswb1tipfdxanx.png)

## Check out your App

After the deployment is successful open your app and check it out. Now you're ready to go 🚀🔥.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tpq08mr2mh1jgluzztt9.png)

## Conclusion

That's it regarding deploying a Django project on Heroku. Here's the project [source code](https://github.com/ThePinger/django_rest_heroku_tutorial) if you wish to check it out. Also feel free to ask any questions 😉.