How To Successfully Deploy Django App On Heroku

How to deploy Django app on Heroku? It is of no use after all the struggle of starting a website from scratch then you still can’t host it to the cloud so that it will be made public to every body.

Before we continue we need to understand what heroku is all about and related cloud to heroku.

There are many hosting company such as Pythonanywhere, Amazon etc. So you might ask your self why Heroku?

What is Heroku
Heroku
is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud,hosting your app on django is very easy ,you just have to follow due steps mentioned in the documentation.

How to deploy your django project on heroku

Steps involve in deploying django App to heroku

  • After succeesfully developed your app and tested it and everything is working fine
  • Install whitenoise

There are 2 ways we can deploy our app.

First Approach

install white noise, add whitenoise to your middleware

pip install gunicorn
pip install whitenoise
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",

pip freeze a requirements.txt file and a Procfile then runtime.txt.This must be created in the folder that is housing your project

mkdir Procfile
pip freeze > requirements.txt
touch runtime.txt

inside the Procfile you are to configure your own to be like this one below.

web: gunicorn mysite.wsgi --log-file -

mysite is where my settings.py is located, remember to add a space to gunicorn as indicated above, in runtime.txt we will specify python version for our website or you can leave it empty heroku will always use the latest version of python for your website.

python-3.9.10

Now we have to set debug=false in settings.py,then our allowed host shouuld be

['yourdomain.com','localhost',]

or you can just leave it at [‘*’], but remember that hackers might use this to penetrate into your website. Now we have to create our statifiles, so set your static files to be like the one below

STATIC_URL = '/static/'
STATIC_ROOT  = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

also configure your template to be like this so that you wont have template issues problems

  'DIRS':[os.path.join(BASE_DIR,'templates')],

Now we Have to create a github account and create a repository then we are going to push our code to github.

I wouldn’t teach you how to do that you can check it out here now after successfully creating a repository in github

Now you have to install git if it is not install, install it pip install git in your environment, or pkg install git. Now when you created a repo in github you will see a code outlined my own is below

echo "# mytuto" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/yourusername/repositoryname.git
git push -u origin main


Go back to vs code or where you were before then run


You must also like how to add ckeditor in your blog or website
git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/yourusername/repositoryname.git
git push -u origin main


if you did any mistake your have to re initialise the git by using git init then git add .

But if you need to delete the git process you have to go to the root of the root of your code and go to open hidden files the remove git files the you will start afresh. if every thing is successful you have to go to github and refresh you will see your code deployed in github.

Now go toheroku.comand create an account then create a new app,googlehow to do thathere

After successful creating account and new app, connect your git hub account to heroku, select the app you have created by taping on deploy then you will see github, and you have to search the repo you have created the if found connect it. then go to settings and tell heroku the app your are deploying,by clicking on add buildstck then select python.

Come back to deploy and hit deploy then in my case every thing work successfully click on your app to see that it is online.

Tips: if you ran into errors things to do is to check your

error logs

your Procfile

your static files

In conclusion to succesful deploy django app on heroku carefully follow the steps above sometimes things really changes . leave us comment below if you encounters any difficulties

Leave a Reply

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