Categories
Django

Deploy Django Project in Heroku

In this post, we will deploy a Django project in Heroku that can be accessed via the internet. Heroku is a cloud-based platform that lets you deploy, run, and manage applications written in different programming languages.

Requirements

For this, we need a Heroku account which is free, Git installed, and a Django project to deploy.

By the end of this post, we will have a domain that serves API via the internet.

We will be using Heroku CLI to deploy Django Project.

Note: Heroku supports deployment of applications written in Ruby, Node.js, Java, Python, Clojure, Scala, Go and PHP.

Set up Heroku CLI

Once you have all the requirements, install Heroku CLI. It is used to manage and scale your applications, provision add-ons, view your application logs, and run your application locally.

Download and install the Heroku CLI in WindowsOS, Ubuntu, and macOS.

After completing installation the command heroku should give you the following output:

heroku cli

Use the heroku login command to log in to the Heroku CLI:

requirement of project

Prepare Django Project

In this step, we will prepare a simple application that can be deployed.

Change directory to project folder.

Install gunicorn package

gunicorn to talk to heroku server

Add a Procfile without any file extension in project folder as:

#Project_folder/Procfile
web: gunicorn drfapi.wsgi

Note: Heroku includes a Procfile that specifies the commands that are executed by the app on startup. 

Add a requirements.txt file in the project that contains all the package needed for the project. You can create it by copying pip freeze list in windows.

#terminal
(venv)$ cd project_folder
(venv)$ pip freeze > requirements.txt

Note: Heroku will install all the dependencies listed in requirements.txt

Add STATIC_ROOT file and ALLOWED_HOSTS in settings.py as all the static files are stored in staticfiles folder.

#settings.py
...
ALLOWED_HOSTS = ['appname.heroku.com']
... 
STATIC_ROOT = os.path.join(BASE_DIR,'staticfiles')

Note: Before Deploying the Django project, you need to check the Django Deployment Checklist such as DEBUG = FALSE in settings.py

Deploy the app

In this step, we will deploy the app to Heroku.

Initialize git in project folder and .gitignore to ignore upload to git.

#terminal
(venv) project_folder $ git init

Commit the changes

#terminal
(venv) project_folder $ git add -A
(venv) project_folder $ git commit -m "message"

Create an app on Heroku:

create projects in heroku

Now deploy your code:

deploy project to heroku

Now visit the app at the URL generated by its app name. As a handy shortcut, you can open the website as follows: heroku open command

you can view logs of your running app using: heroku logs --tail

Provision a database

In this step, we will learn about the free Heroku Postgres add-on that was automatically provisioned when your app was deployed.

We can view the database info as: heroku pg

heroku addons info

Note: your projects should be configured to the Postgres database.

#settings.py
. . .
#add pip install psycopg2
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'project_name',
        'USER': 'user_name',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}
. . .

Run the standard Django manage.py migrate to create the tables.

database migrate in addons of heroku

you can access the project via internet:

access heroku app via internet

Note: For storing images and static files AWS S3 bucket is recommended.

Wrapping up

In this post, we learn to deploy Django project in Heroku. We looked at how to setup a Heroku server and host a django project including a database in detail.