Step-by-Step Guide to Deploying Django on Heroku in 2025

By Nischal Lamichhane

20 reads 0 comments 7 likes

Step-by-Step Guide to Deploying Django on Heroku in 2025

Published on January 26, 2025


Deploying a Django Web Application on Heroku (2025)

 

 

  1. Sign Up for Heroku:

    Create an account at Heroku Signup.

  2. Install Git:

    Download and install Git from the official website.

  3. Install the Heroku CLI:

    Follow the instructions on the Heroku CLI documentation to install the Command Line Interface.

  4. Prepare Your Django Project:
    1. Open your terminal and navigate to your project directory:

      cd /path/to/your/project
    2. Ensure your virtual environment is activated and install the necessary packages:

      pip install gunicorn psycopg2-binary

      Note: psycopg2-binary is required for PostgreSQL database support.

    3. Freeze your dependencies into a requirements.txt file:

      pip freeze > requirements.txt
  5. Configure settings.py:
    1. Import necessary modules at the top:

      import os
      from pathlib import Path
      import dj_database_url
    2. Set the ALLOWED_HOSTS to include your Heroku app's domain:

      ALLOWED_HOSTS = ['your-app-name.herokuapp.com']
    3. Configure the database settings to use Heroku's PostgreSQL:

      DATABASES = {
          'default': dj_database_url.parse(os.environ.get('DATABASE_URL'))
      }
    4. Configure static files settings:

      STATIC_URL = '/static/'
      STATIC_ROOT = Path(BASE_DIR) / 'staticfiles'
    5. Set the secret key and debug mode using environment variables:

      SECRET_KEY = os.environ.get('SECRET_KEY', 'your-default-secret-key')
      DEBUG = os.environ.get('DEBUG', 'False') == 'True'
  6. Create a Procfile:

    In your project's root directory, create a file named Procfile (without any extension) and add the following line:

    web: gunicorn your_project_name.wsgi

    Replace your_project_name with the name of your Django project directory.

  7. Initialize a Git Repository:
    1. Initialize the repository:

      git init
    2. Add all files:

      git add .
    3. Commit the changes:

      git commit -m "Initial commit"
  8. Deploy to Heroku:
    1. Create a new Heroku app:

      heroku create your-app-name

      Replace your-app-name with your desired app name.

    2. Set environment variables:

      heroku config:set SECRET_KEY='your-secret-key'
      heroku config:set DEBUG='False'
    3. Push your code to Heroku:

      git push heroku master
    4. Run database migrations on Heroku:

      heroku run python manage.py migrate
    5. Create a superuser for the admin interface:

      heroku run python manage.py createsuperuser
    6. Open your application in the browser:

      heroku open

For a comprehensive guide, refer to the official Heroku documentation on Configuring Django Apps for Heroku.

Notes:

  • The django-heroku package is no longer recommended. Instead, configure your settings manually as outlined above.
  • Ensure that your SECRET_KEY and other sensitive settings are managed securely using environment variables.
  • Regularly update your dependencies and configurations to align with the latest best practices and security recommendations.

For a visual walkthrough, you might find this video helpful:

Comments

You must be logged in to post a comment.


No comments yet. Be the first to comment!

Also Read

Mastering Python Command-Line Arguments: A Comprehensive Guide
Mastering Python Command-Line Arguments: A Comprehensive Guide

Learn how to use Python command-line arguments effectively to automate tasks, streamline workflows,…

Create the viral Ghibli Art for FREE
Create the viral Ghibli Art for FREE

How to create your own Ghibli Art for Free!

Integrate HTMX with Django: A Modern Alternative to ReactJS
Integrate HTMX with Django: A Modern Alternative to ReactJS

Discover how to integrate HTMX with Django to build modern, interactive web applications. Learn to …

Deploying Django Apps for Free on PythonAnywhere: Step-by-Step Guide
Deploying Django Apps for Free on PythonAnywhere: Step-by-Step Guide

Learn how to deploy Django apps for free on PythonAnywhere with this step-by-step guide. From proje…

Flask Vs Django
Flask Vs Django

This article provides a comprehensive comparison between Flask and Django, two prominent Python web…

Python Heap - Complete Guide to Heap Data Structures in Python
Python Heap - Complete Guide to Heap Data Structures in Python

Learn everything about Python Heap, including heap data structures, the heapq module, min-heaps, ma…