Open In App

Configuring Django Apps for Heroku

Last Updated : 11 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to configure a Django app for Heroku step-by-step. 

Heroku is a cloud-based application deployment and management service which supports several programming languages such as Ruby, Java, Node.js, Scala, Clojure, Python, PHP, and Go. It requires a Procfile, this file is used to declare your entry points and application’s process. 

Stepwise Implementation

Step 1: Create Environment: 

Create a virtual environment in your Django project folder. Run the following command:

python -m venv .venv

Step 2: Virtual Environment: 

Enter the created environment with the help of the below command: 

source .venv/bin/activate

 

Step 3: Install Dependencies:

 Install all the packages and libraries required to run your web application along with gunicorn.

Note: You might require to install more dependencies besides Django if your application is using any. For example djangorestframework

pip install django gunicorn

 

Step 4: Requirements file: 

Create a “requirements.txt” file so that Heroku knows all the dependencies it needs to run our application.

pip freeze > requirements.txt

 

Step 5: Procfile: 

Create a file named “Procfile” within your root directory and write the following:

web: gunicorn [project-name].wsgi --log-file -

 

Step 6: Runtime file: 

Create a new file named “runtime.txt” within your root directory,  It tells Heroku what version of Python your application is using. 
You can check your version by following the command “python –version” and pasting it into the runtime.txt.

python-3.x.xx

 

Step 7: Debug Settings: 

Navigate yourself to the settings.py file under projects and set debug value False (Default = True).

Note: Never deploy a site into production with DEBUG turned on.

DEBUG = False

 

Step 8: Allowed Host: 

Add your Heroku app URL and localhost in the Allowed Host list which is within your settings.py file.

You can also  allow all apps from Heroku by omitting the app name like this:

ALLOWED_HOSTS = [‘.herokuapp.com’] and You can also use [‘*’] to allow all hosts.

ALLOWED_HOSTS = ['127.0.0.1', '<HerokuAppName>.herokuapp.com']

 

Now your Django application has been configured to be deployed on Heroku. So you can go ahead and deploy.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads