In this tutorial we will learn how to deploy your django app on heroku using github.

Setting up Django Project
Firstly we will create a django project or one may also use any existing project. In case you don’t know how to create a django project you may refer our previous blogs .
In my case I have created an app name demo and an app name basicapp. So, my structure is demo/basicapp
.
Once we are done with creation of django app we have to setup a new app on Heroku.
Setting A New App On Heroku
After login to Heroku you need to click ‘create new app‘ after this you have to name your app . In my case I have name the app as ‘studygyaantesting‘ .
We are now done with creating a new app now we have to connect our github repository with heroku as shown below

Some Final Changes In Django Project
Now, we are done with setting up an app on heroku, now all we want is to deploy our project on that app.
For this we will first install some packages
pip install gunicorn pip install whitenoise
After this we will be making following changes in settings.py
- We will add path to STATIC_ROOT
STATIC_ROOT=os.path.join(BASE_DIR, 'static')
2. Adding ‘whitenoise.middleware.WhiteNoiseMiddleware’ in MIDDLEWARE
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ]
3. Adding your app name to ALLOWED_HOSTS. In my case I have used my app name i.e. studygyaantesting.herokuapp.com
ALLOWED_HOSTS = ['studygyaantesting.herokuapp.com']
Creating Procfile
After making the above changes in settings.py
we now have to create a file in root directory of our project (where manage.py is located).
We will create a text document and write ‘ web: gunicorn <your project name>.wsgi –log-file – ‘ . In my case I have written
web: gunicorn demo.wsgi --log-file -
After writing this save this as Procfile .
Remember- The Procfile should not have any extention i.e we have to remove .txt extention while saving.
Creating requirements.txt
After we create the procfile now we are almost done, at the end we have to run the command
pip freeze > requirements.txt
Now, we will just push our project to Github.
Deploying to Heroku
Once we have pushed our code to github we just have to go our heroku app and click on deploy section. Afte that we will see a button ‘Deploy Branch’. We have to just click that and our project id deployed now.


GitHub – Run Example Locally
GitHub – https://github.com/priyanshuarora1/herokudemo.git
Clone the Repository
git clone https://github.com/priyanshuarora1/Protecting-Django-Project-From-Getting-Attacked.git
Create Virtual Environment – VirtualEnv
mkvirtualenv env
Run requirements file to install libraries using pip
pip install -r requirements.txt