Skip to content
Home » Programming » Django » How to deploy your Django project in Heroku

How to deploy your Django project in Heroku

django project in heroku

Heroku is a cloud platform as a service supporting several programming languages. One of the first cloud platforms, Heroku has been in development since June 2007, when it supported only the Ruby programming language, but now supports Java, Node.js, Scala, Clojure, Python, PHP, and Go. Deploying your Django project in Heroku for both developmental and production stage is pretty straightforward. “How to deploy your Django project in Heroku” tutorial provides you step by step guide to deploy your Django project in Heroku. You can follow these steps to deploy your Django project in Heroku :

(This tutorial is based on windows operating system. But the general idea is the same and any OS’s user can follow along quite easily)

1. Make Backup

It is always a good idea to back up your project before deployment. So make a copy of your project and work with it for this tutorial.

2. Install Heroku CLI and Git

You can install Git by following the Git website.

Heroku provides a easy to use command line interface called Heroku CLI. Heroku CLI requires Git. So make sure you install Git before this step. You can find detailed article on installing Heroku CLI from Heroku website.

3. Activate virtual environment

To learn more about Creating and Using Virtual Environment follow along Installing package using pip and Virtual Environment article.

You need to now activate virtual environment for your project. You can do this by using the following command in Command prompt or Power-Shell.

\path\to\env\Scripts\activate

4. Install gunicorn and django-heroku in your virtual environment

pip install gunicorn
pip install django-heroku

Also read : Installing packages using pip and Virtual Environment

5. Edit settings.py

Add the following import statement to the top of settings.py :

import django_heroku

Then add the following to the bottom of settings.py :

django_heroku.settings(locals())

6. Create Procfile

Create a file named Procfile without any extension at the root of your project and write the following lines in that file using any preferred text editor.

- web: gunicorn <project_name_here>.wsgi --log-file -

note: ignore the dash(-) at the beginning

7. Create requirements.txt

Use following command to create requirements.txt

- pip freeze > requirements.txt

8. Login Heroku and create Heroku app using Command-Line

heroku login
heroku create <your app name>

9. Add PostgreSQL database

Go To Heroku website and click on add-ons. Then search for PostgreSQL and add it.

After it is added then go to settings of the PostgreSQL database just created and view Credentials to get the detail about host, username, password, database_name, port_number.

Then edit the database section of the settings.py to add PostgreSQL database setting replacing SQLite database setting.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': '<database name>',
        'USER': '<database username>',
        'PASSWORD': '<database password>',
        'HOST': '<database host>',
        'PORT': '<database port>',
    }
}

10. Finalize Deployment (part 1)

Before continuing with step 10 make sure GIT is already installed in your system.

Change your directory to root of project and then user the following commands.

git init
git add .
git commit -m "some text"
heroku git:remote -a <your_app_name>
git push heroku master

11. Finalize Deployment (part 2)

At this point your project is deployed but needs some more commands Heroku bash (command-line) for that: Go to Heroku website and use following commands in the Heroku bash.

python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser

Your Heroku web-app is ready and you can visit it by going to following site.

<your_app_name>.herokuapp.com

Leave a Reply

Your email address will not be published. Required fields are marked *