Tuesday, November 19, 2024
Google search engine
HomeLanguagesHow to Deploy a Django Application to Heroku with Git CLI?

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

Django is a popular web Framework of Python. It is based on MVT(Model-View-Template). MVT is a software design pattern for developing a web application. For more details about Django visit the Django tutorial.

Deploying Django project using Heroku here is two methods:

  1. Deploying Django project on Heroku using CLI 
  2. Deploying Django project on Heroku using Git CLI

For more details about the First method visit the Deploy Django project on Heroku using CLI

Here we discuss the second method How to deploy Django project on Heroku using Git CLI, It contains both the Project side Heroku sidestep:

Project Side Setup:

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

Step 1: For Heroku Deployment First we need to install dependencies [library] :

  1. Django-Heroku
  2. gunicorn
  3. whitenoise (It help to connect your project with the server)

Note: Dependencies are always dependent on the project, here mentioned dependency has commonly used in Django apps when you deploy a project on Heroku. For installing dependencies you used the pip command :

pip install <library name> 

Step 2: Create required files.

Heroku basically required two files Procfile and requirements.

  • Procfile : procfile is created at manage.py file directory, procfile does not require any extension :
web: gunicorn <project name>.wsgi --log-file -
  • Requirement: The requirement file will store all the dependencies and their versions regarding the project.

Run following command in cmd:

pip freeze > requirements.txt 

Step 3: Update setting.py file

  • Import django_heroku at the top, it is the configuration of the Django application.
import django_heroku
  • Set DEBUG = False its hides the URL link at the dynamic side.
DEBUG = FALSE
  • Add app URL in allowed hosts
ALLOWED_HOSTS = ["*"] also you pass the url link of the project 
                    OR
ALLOALLOWED_HOSTS = ["https://elitebatch.herokuapp.com/"]
  • Add whitenoise middleware 
"whitenoise.middleware.WhiteNoiseMiddleware"
  • Add static root (it is required when you use static storage in your project)
  • Add django_heroku setting at the last django_heroku.settings(locals())
django_heroku.settings(locals())

Step 4: Upload your project on GitHub.

Heroku Side Setup: 

  1. First of all, create Heroku Account.
  2. Create an app on Heroku 
  3. Add python build pack
  4. Link your Heroku app with the Github repository – go to your app Heroku desktop and go to the deploy tab select deployment method (Github). After successfully connecting with the deployment method scroll down and search the repository that you want to connect with your app.
  5. After Linking with Github, choose the branch, Heroku gives us two methods either manually or turn on automatic deployment 

Great You have successfully uploaded your project on Heroku using git CLI. Project Live on Heroku and source code are available at Github

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments