Deploy python django app in heroku app

Heroku is a cloud platform which provides Platform as Service (PaaS) unlike Amazon Web Service (AWS) which is Infrastructure as a Service (IaaS). Heroku free tier provide free service with limitation. If you are planning to host for production in Heroku, you have to purchase Dynos. Dynos are something like docker container which is managed by Heroku. 

Heroku removes the headache of maintaining Infrastructure by ourselves and letting us to focus on app development. I always use Heroku for prototyping app and get it visible quickly.

You can host up to five apps for free account. We will see how to deploy your Django app in Heroku by Heroku-cli and Git.

Deployment Strategies

You can deploy your Django application in three ways.

  1. Deploying using Heroku-cli
  2. Deploying using GitHub
  3. Deploying using docker container

In this post, We will focus on how to deploy using GitHub and cli. My preferred method is using GitHub as I use GitHub for all my projects and It is easy to maintain as well. Both method requires creating git repository.

Regardless these two methods, app must be pre-configured to work with Heroku. We will see those deployment methods below.

Installing dependencies

Gunicorn and django_heroku packages needs to be installed for deploying in Heroku.

django_heroku - configures your django app for deploying in Heroku environment. It sets database URL from Heroku, environment variables set in Heroku.

gunicorn - is the application server for WSGI application.

(blog)   thuruthuru git:(heroku_depolyment) pip install django_heroku gunicorn

Create requirements file

It is important to create requirements.txt file. Heroku will identity app as python app with requirements.txt file and install Django app dependencies. You can use below command to create requirements files.

(blog)   thuruthuru git:(heroku_depolyment) pip freeze > requirements.txt

Configuring settings.py

Open your settings.py from project directory and add below configuration at the end script. This sets database URL from Heroku, environment variables set in Heroku.

# Heroku deployment configuration
import django_heroku
django_heroku.settings(locals())

Creating Procfile

Procfile is important for deploying django application with gunicorn. Profile has gunicorn command to run the WSGI application. Since our application is Django web app. We need to define web app in Procfile. Replace YOUR_APP_NAME with your app name. Procfile should be without any extension.

web: gunicorn YOUR_APP_NAME.wsgi

So far what we have done will be general to heroku-cli and github deployment.

Configuring Heroku-cli

You need Git repo created and Heroku-CLI to be installed in your local machine. You will need Heroku-CLI to run migration scripts for Django. You can download Heroku client from official site. Login to Heroku account using command line with your email address.

(blog)   thuruthuru git:(heroku_depolyment) heroku login

Creating Heroku app

You can create Heroku app from website and using CLI. 

Using website

  1. Login to Heroku dashboard.
  2. Click New and Create New app.
  3. Provide unique app name to create.

Using CLI

You can skip this step if you already created your app. Replace YOUR_HEROKU_APP_NAME with your app name.

(blog)   thuruthuru git:(heroku_depolyment) heroku apps:create YOUR_HEROKU_APP_NAME

Sample output of the above command. 

Creating thuruthuru... done

https://thuruthuru.herokuapp.com/ | https://git.heroku.com/thuruthuru.git

Deploying using Heroku-cli

Create GIT repo

If you haven't create a git repo, create git repository and add all the files to repository.

(blog)   thuruthuru git:(heroku_depolyment) git init

(blog)   thuruthuru git:(heroku_depolyment git add .

(blog)   thuruthuru git:(heroku_depolyment git commit -m "initial commit"

Add Heroku repository to our local git to push code to Heroku remote repository.

(blog)   thuruthuru git:(heroku_depolyment) heroku git:remote -a YOUR_HEROKU_APP_NAME

Above command output to my repo.

set git remote heroku to https://git.heroku.com/thuruthuru.git

Push code to Heroku

Once you mapped to your repository to Heroku remote repository, you can push to Heroku.

(blog)   thuruthuru git:(heroku_depolyment git push heroku master

Heroku needs master branch to deploy. If you are using different branch for deployment, you can map to master and push. Now I am pushing my heroku_deployment branch to master branch in Heroku.

(blog)   thuruthuru git:(heroku_depolyment) git push heroku heroku_depolyment:master

As soon as you push the code to Heroku, Heroku identifies your app, installs dependencies and collectstatic. If you need any configurations from environment variables, collectstatic may fail as we didn't set any environment. We will set environment variables in next section and push the code again.

With this we have done our deployment with Heroku-CLI. Now we will see how to deploy from GitHub repo.

Setting Environment variables

  1. Go to your Heroku app dashboard. Click on your app and navigate to settings.
  2. Go to Config Vars and click Reveal Config Vars. Add your environment variables

  3. Now you deploy if you had failure while deploying using Heroku-CLI.

Deploying with GitHub

Let's say you have heroku_deployment repo in GitHub which you wish to deploy in Heroku.

  1. Go to deploy under your Heroku app and click "GitHub"
  2. Connect to your github repository.
  3. Once you connect your heroku app to github repository. You will have to options to deploy from.
    1. Automatic deployment - automatically deploys your branch from github whenever you push new changes.
    2. Manual deployment - You will have to pick branch to deploy.

Running Migration

Once the deployment is completed, you can run your python command as below.

(blog)   thuruthuru git:(heroku_depolyment) heroku run python manage.py migrate

Now application has been deployed to Heroku and accessible in the internet.

Accessing Heroku App

URL will be your_heroku_app_name.herokuapp.com. You can get this link from app settings in Heroku website.

  1. You will be able to open your app from dashboard.

  2. You will be able to get the app url under settings from Domain.

Now your app will be ready to access online.

Debugging in Heroku

You will be able to view the logs of the deployment with below command in the shell.

(blog)   thuruthuru git:(heroku_depolyment) heroku logs --tail --app YOUR_HEROKU_APP

You will be able to view same deployment logs in activity under your app. In case if you want to check your application errors, CLI will be helpful.

Caution

If your app needs user uploaded content to work, You need to use S3 for file storage. Heroku filesystem will not keep your media files, as free heroku account dyno shutsdown after some time of inactivity and redeployed when it becomes active. You can take a look at my other post to configure S3 for media files.

Enjoy your app now!! Let me know in comments if you follow some other strategy to deploy or have any other tips to share.

Related posts
Managing media and static files in Django using S3

Managing media and static files in Django using S3

Durai Pandian May 10, 2020

Managing Django media files and static files from AWS S3 using django-storages instead of serving them from the same ser...
Continue reading...
AWS S3 configuration for backup

AWS S3 configuration for backup

Durai Pandian May 07, 2020

Amazon Simple Storage Service (AWS S3) configuration for backup with expiration, maintaining versions and configuration ...
Continue reading...
Django simple deployment with gunicorn and whitenoise

Django simple deployment with gunicorn and whitenoise

Durai Pandian May 03, 2020

Django simple deployment using gunicorn and whitenoise serving static files with STATIC_URL, STATIC_ROOT, collectstatic....
Continue reading...

Comments
We'll never share your email with anyone else.