Deploy a Django app on Heroku from a GitHub repo
Tags: django, heroku, github
Published: 2020-01-24 06:47:10

In this guide we are going to show you how to deploy a Django application on Heroku from a GitHub repository step by step.

The points we are going to cover in this guide are:

* Take care of Static files

* Configure Debug mode using environmental variables

* Configure Allowed Hosts

* Create a Heroku Dyno

* Create a new app on Heroku

* Connection to GitHub repository and deployment

In order to deploy your project, we need to make a few changes in the code. For the ones who don’t have experience in deploying applications it might seem a bit complicated at first but trust me, the next times it’s going to be much smoother.

## Static Files

The hosted application has to collect and serve the static files (media, css etc.) in the browser. Make sure the *settings.py* has the following lines of code.

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    STATIC_URL = '/static/'
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'),
    )

Also, Django does not support serving static files in production automatically so we install [WhiteNoise](http://whitenoise.evans.io/en/stable/index.html). Don’t forget to add it in the *requirements.txt* file after the installation is done. Let’s run the commands in our terminal!

    pip install whitenoise
    pip freeze > requirements.txt

And let's add it in the MIDDLEWARE components in *settings.py*,


    MIDDLEWARE = ['whitenoise.middleware.WhiteNoiseMiddleware']

## Debug mode

When we run the application locally, the DEBUG mode is True, but when we run it in **production** it should be **False**. Imagine all the users being able to see the debug messages! But what if we want to use the same repo for both production and local development. How can we do that? How can the DEBUG mode take the right value depending the environment? For that case we take advantage of the environmental variables. Let’s see how we can use them.

In the *settings.py* file

    import os
    DEBUG = os.environ.get(‘DEBUG_VALUE’)

In Heroku’s settings we can easily configure the environmental variables, where in our case we set the **DEBUG_VALUE** to **FALSE.**

![](https://cdn-images-1.medium.com/max/2880/1*Xa2GsakS4Q_Ni7P7noE8cA.png)

![](https://cdn-images-1.medium.com/max/3500/1*9Hc9gcRDMjFN7Irhd51LCA.png)

Accordingly, for our local machine we can use the wanted value for the debug mode in our operating system’s environmental variables file, like below:

    export DEBUG_VALUE=”True”

## Allowed Hosts

Now we are going to configure the Allowed Hosts in the *settings.py* file. The first one is the Heroku domain and the second one is for running the application locally.

    ALLOWED_HOSTS = [‘smart-flight-search.herokuapp.com’, ‘0.0.0.0’]

## Set up Dyno & Procfile

In order to execute the application we need to configure a Heroku Dyno, which is mainly an instance of a server.

Now let’s create a file called **Procfile **in the root directory of the project.

    web: gunicorn -w 2 --chdir amadeus_demo_api/  amadeus_demo_api.wsgi:application --reload --timeout 900

If you want to check locally the Procfile is configured properly, you can run the above command in your terminal. Just don’t forget to install [gunicorn](https://gunicorn.org/) and add it in the requirements file.

## Create the new application

Now it’s time to move on with the actual deployment process on Heroku! After we login in Heroku web, we can click on **create new app** and we choose a unique app name also the region.

![create-heroku-app](https://cdn-images-1.medium.com/max/3140/1*o9hMeX4kueSSD8JA7DLpvw.png)*create-heroku-app*

## Connect to GitHub

When the app is created, we have to go to the **Deploy tab** and choose **GitHub** as a deployment method. Then we choose the repository with the project we want to host on Heroku.

![connect-repo](https://cdn-images-1.medium.com/max/5168/1*24_unYV3LKImfK8n5jJ3zg.png)*connect-repo*

As we can see, we have the option to choose automatic deployment or manual one. That really depends on you, but you have to make sure your branch is always in the right state in case you chose the automatic option, as it’s going to be deployed every time you push to your branch.

![](https://cdn-images-1.medium.com/max/5024/1*r-fp548lFIdjL5d_bVhaaQ.png)

If we choose manual deployment we have to click on Deploy Branch and that’s it! Heroku displays the log of the deployment process until it’s done so we can see if it’s successfully executed or if there are errors.

Sometimes the application has been deployed but when we go to the page we can see there are errors. This can happen for many reasons, for example problem with the dynos, the allowed hosts etc. In order to view the errors while our server is running the best way is to install Heroku on your terminal and check the logs with the following command:

    heroku logs — app APP_NAME

## Conclusion

In this guide we went through the steps we have to do in order to deploy a Django project from a GitHub repository on Heroku. We saw the changes we have to do in our code to make it work properly in production and then how we can deploy the application via the Heroku webpage.