Deploy Your First Django App With Heroku
Tags: webdev, python, django, html
Published: 2020-08-18 14:02:17

This article was originally published on [CodePerfectPlus.com](http://codeperfectplus.herokuapp.com/deploy-your-first-django-app-with-heroku)

Heroku is a cloud platform as a service supporting several programming languages. They support several programming languages like Python, Javascript, Java, Php, Go, Scala and Ruby.
Heroku offer a free plan, it's great to start learning and for the host demo application.

### Install Heroku CLI Tool

Create an account on <a href="https://www.heroku.com/" target="_blank">Heroku</a> and then install the Heroku CLI tool from <a href="https://devcenter.heroku.com/articles/heroku-cli" target="_blank">here</a>. It's Command line tool to deploy and manage your Django application.

<div class="code p-3 ml-2">
```bash
$ heroku login
heroku: Press any key to open up the browser to login or q to exit: 
```
</div>

### Prepare the Django Application

In this tutorial, I will deploy my existing project of GitHub API. It's GitHub API project developed 6 months ago and it's also available on <a href="https://github.com/codePerfectPlus/Deploy-Django-Applications-on-Heroku" target="_blank">Github</a> and you can clone the repository and try it by yourself.

I will be using Heroku CLI with Git. Your application will be saved in a remote git repository in the Heroku Cloud.

Before starting You Have to add some file to your projects.

-   Add a  **Procfile**  in the project root;
-   Add  **Gunicorn**  to  **virtualenv**;
-   A  **runtime.txt**  to specify the correct Python version in the project root;
-   Configure  **whitenoise**  to serve static files.

### The Procfile
Create a file named **Procfile** in the project root with the following content:

<div class="code p-3 ml-2">
```bash
web: gunicorn base.wsgi --log-file -
```

</div>

Note: change **base** with the name of your Django project.

### Gunicorn
install Gunicorn in VirtualEnv by command:

<div class="code p-3 ml-2">
```bash
$ pipenv install gunicorn
$ pipenv install whitenoise
```
</div>

### The runtime.txt
create a filename `runtime.txt`

<div class="code p-3 ml-2">
```bash
python-3.7.6
```
</div>

### Set Up The Static Assets
Configure the STATIC-related parameters on  **settings.py**:

Add whienoise to settings.py then in the last of MIDDLEWARE List.
<div class="code">
```bash
"whitenoise.middleware.WhiteNoiseMiddleware",
```
</div>

## Deployment
Almost all configurations are completed.

clone the Repository here if you haven't downloaded from upper link.

<div class="code p-3 ml-2">
```bash
git clone https://github.com/codePerfectPlus/Deploy-Django-Applications-on-Heroku
cd Deploy-Django-Applications-on-Heroku
```
</div>

#### Login to Heroku CLI

<div class="code p-3 ml-2">
```bash
$ heroku login
```
</div>

#### Create a Heroku App
This will be your app name. appname.herokuapp.com.
choose clearly the name for the app.

<div class="code p-3 ml-2">
```bash
$ heroku create gitapi-project
```
</div>

#### Output
<div class="code p-3 ml-2">
```bash
Creating ⬢ gitapi-project... done
https://gitapi-project.herokuapp.com/https://git.heroku.com/gitapi-project.git
```
</div>

You can simply run `heroku create` then Heroku will find the app name by itself.

Now login to [Heroku Dashboard](https://id.heroku.com/login) and access your recently created app:

#### Click on Deploy

Run the following command for the deployment of Django App.

<div class="code p-3 ml-2">
```bash
heroku config:set DEBUG_COLLECTSTATIC=1

$ git add .
$ git commit -am "make it better"
$ git push heroku master
```
</div>

Your Application in Live. Enjoy Your Day, Don't forget to comment.

### More Articles by Author

- [Deploy Your First Django App With Heroku](http://codeperfectplus.herokuapp.com/deploy-your-first-django-app-with-heroku)
- [Single-layer Neural Networks in Machine Learning (Perceptrons)](http://codeperfectplus.herokuapp.com/neural-networks-in-machine-learning-perceptrons)
- [5 Tips for Computer Programming Beginners](http://codeperfectplus.herokuapp.com/5-tips-for-computer-programming-beginners)
- [What Is Git and GitHub?](http://codeperfectplus.herokuapp.com/what-is-git-and-gitHub)
- [What is Simple Linear Regression?](http://codeperfectplus.herokuapp.com/what-is-simple-linear-regression)
- [Introduction to Machine Learning and it's Type.](http://codeperfectplus.herokuapp.com/introduction-to-machine-learning-and-its-type)
- [Difference Between Machine Learning and Artificial Intelligence](http://codeperfectplus.herokuapp.com/difference-between-machine-learning-and-AI)

### Join for Weekly Updates.

- [Whatapp Group](https://chat.whatsapp.com/H8lJop5N3AI0BgY9CbKze8)

- [Telegram Channel](https://t.me/codeperfectplus)