VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-deploy-a-django-application-to-heroku-with-git-cli/

⇱ How To Deploy a Django Application to Heroku with Git CLI? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How To Deploy a Django Application to Heroku with Git CLI?

Last Updated : 9 May, 2026

Heroku is a platform-as-a-service (PaaS) for deploying, managing, and scaling applications. There are two ways to deploy a Django project on Heroku:

  • Deploying Django project on Heroku using CLI 
  • Deploying Django project on Heroku using Git CLI

This section explains how to deploy a Django project to Heroku using Git CLI, covering both project setup and Heroku configuration.

Prerequisites

Before you begin, make sure you have:

  • A working Django project
  • Git installed and configured
  • A Heroku account

Steps to Deploy Django Application to Heroku

Follow the below steps to deploy your Django project to Heroku using Git CLI:

Step 1: Install Required Dependencies

Install the essential libraries needed for deployment:

  • gunicorn : WSGI HTTP server for running your app
  • whitenoise : Serves static files in production
  • dj-database-url : Configures database via environment variables

Run:

pip install gunicorn whitenoise dj-database-url

Note: Additional dependencies may be required depending on your project.

Step 2: Create Required Files

  • Procfile: Create a file named Procfile (no extension) in the root directory (same level as manage.py):

web: gunicorn <project_name>.wsgi

  • requirements.txt: Generate a list of dependencies

pip freeze > requirements.txt

Step 3: Update setting.py

Disable Debug Mode

DEBUG = FALSE

Configure Allowed Hosts

ALLOWED_HOSTS = ["<your-app-name>.herokuapp.com"]

Add WhiteNoise middleware 

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
...
]

Configure Static Files

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

Configure Database (PostgreSQL).

import dj_database_url
DATABASES = {
'default': dj_database_url.config(conn_max_age=600, ssl_require=True)
}

Step 4: Prepare Your Project for Deployment

Initialize Git (if not already done):

git init

git add .

git commit -m "Initial commit"

Step 5: Set Up Heroku

  • Log in to your Heroku account
  • Create a new app
  • Add the Python buildpack (usually added automatically)

Step 6: Connect Heroku to GitHub (Optional but Recommended)

  • Go to the Deploy tab in your Heroku dashboard
  • Select GitHub as the deployment method
  • Connect your repository

You can then choose:

  • Manual Deploy : deploy on demand
  • Automatic Deploys : deploy on every push

Step 7: Deploy Using Git CLI

  • Login to Heroku via CLI:

heroku login

  • Create an app (if not already created):

heroku create <your-app-name>

  • Push your code:

git push heroku main

Step 8: Run Migrations

heroku run python manage.py migrate

Step 9: Open Your Application

heroku open

Optional: Collect Static Files

If needed, run:

heroku run python manage.py collectstatic

Final Notes

  • Ensure all environment variables (like SECRET_KEY) are set using Heroku config vars
  • Monitor logs using:

heroku logs --tail

  • For production, consider adding proper error handling and security settings
Comment
Article Tags: