Deploying Django app on Heroku with GitHub

Mrudula M
Analytics Vidhya
Published in
5 min readJul 11, 2020

Heroku provides many powerful features for deploying a project up on a live server to access it from anywhere in the world. The easiest way is to integrate it with GitHub and deploy code living on GitHub. Heroku can automatically build and release (if the build is successful) pushes to the specified GitHub repository.

Prerequisites : GitHub Account and Django App.

Here is the list of things we would be doing-

  • Create an app on Heroku
  • Prepare the app for deployment
  • Push code to GitHub
  • Deploy on Heroku

Lets get started!

1. Sign up and create an app on Heroku

Sign up for a free heroku account by entering your details here.

Login to your account and click on New> Create new app.

Enter a unique App name. (it might take a few tries sometimes to see what’s available). Check region and click on Create app.

2. Preparing Django App for deployment

Preparing Django App for deployment consists of adding a few files to project root directory, installing few packages and configuring the settings.py file for heroku deployment. Dive right in and follow the steps-

In Django settings.py, set debug value to False and in ALLOWED_HOSTS, enter your heroku app domain name that we just created above.

DEBUG = FalseALLOWED_HOSTS = ['mrmapp.herokuapp.com']

Now, install django-heroku package. This automatically configures your Django application to work on Heroku.

pip install django-heroku

At the top of settings.py file import django_heroku and add the following statement at the bottom of the file.

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

Next, install the below packages-

pip install gunicorn
pip install whitenoise

Gunicorn is a Python-HTTP server for WSGI applications and WhiteNoise allows our web app to serve its own static files. Now in your project settings.py add ‘whitenoise.middleware.WhiteNoiseMiddleware’, in the MIDDLEWARE section as shown below-

As Heroku doesn't serve static files, we need to configure STATIC_ROOT in our project settings.py file by adding the below line above our static_url

Next, create a file called “Procfile” without any file extension in your app’s root directory(which contains manage.py). It specifies the commands that are executed by the app on startup. Here, we specify a process type by adding the below line in the file

web: gunicorn projectname.wsgi --log-file -

Note: project name should be the name of the folder containing the wsgi file, settings.py file

Next, create a file runtime.txt in django project root directory(where manage.py file is present). This tells Heroku what version of python we are using. Enter the version of python you are using in the file.

Now, run the below command in the terminal

pip freeze > requirements.txt

This creates a requirements.txt file in the root directory(same as that containing manage.py file) and the file contains the list of all packages and their versions that are needed to make the project work. All the dependencies specified in the file are automatically installed before the app startup in heroku.(If you install new packages and run the same command, it would update the existing file)

This concludes the project settings and next we will push the code to GitHub and deploy it.

3. Push the code to GitHub

I used the PyCharm IDE and created a private repository and pushed my code to GitHub. For this, in the menu bar navigate to VCS> Import into Version Control> Share project on GitHub. Enter a repository name , check on private and Share. See details here.

Another alternative is, Create an empty private repository in your GitHub account. Drag and drop the files (leaving the virtual environment ENV folder) from your project folder to the repo directly and commit them to the master branch.

If you have an existing repo, then push the latest code and merge it to the master branch, as we would be deploying the master branch itself.

4. Deployment in Heroku

In Heroku, navigate to your app> Settings. Scroll down and click on Add buildpack> Select Python > Save changes

Navigate to Deploy tab, under Deployment method> click on GitHub. Click on Connect to GitHub button below. A pop up appears asking for authorization to connect to your GitHub account. Click on Authorize Heroku. In the Connect to GitHub section, search for your repository. Click on Connect.

Once connected, you have options to deploy it Manually or Automatically. With manual deploys, you can create an immediate deployment of any branch from the connected repository. Use manual deploys if you want to control when changes are deployed to Heroku.

Automatic deploy can be used, if you want to deploy every time a new push is made to master. When you enable automatic deploys for a branch, Heroku builds and deploys all pushes made to that branch automatically.

Select the branch (master) and click on Deploy Branch. This starts the build process and once done, a success message is displayed.

Click on the View button. This should launch your app in the browser, Your app is now up, live and running and can be accessed from anywhere around the world, Congratulations !

P.S: If you want to host your database on AWS and connect it to heroku, you can follow my blog here. Happy reading !

Photo by Ben Kolde on Unsplash

--

--

Mrudula M
Analytics Vidhya

An aspiring Python Developer and QA Engineer, Passionate about technology and learning. Mom to an awesome son.