DEPLOYING DJANGO WEB APP ON HEROKU
Signup on Heroku
Install Heroku CLI (Command Line Interface)
Open Terminal and navigate into your project
Install “gunicorn” package by using the command pip install gunicorn
Install django heroku package by running the command pip install django-heroku
Go to settings.py file in your project and add the following lines
import django_heroku (add this after import.os line)
django_heroku.settings(locals()) (Scroll down to the bottom of settings.py page and add this line)
Now go back to your terminal and check the list of all packages used in your project using the command pip freeze
Creating requirements.txt file
Use this command pip freeze > requirements.txt to automatically create requirements.txt file in your project
Create a “git ignore” file in your project manually and copy the code from this link. File name should be as .git ignore
Create a file with the name Procfile in the root directory of your project. Open the Procfile and write the following line.
web : gunicorn projectname.wsgi
(( Note: project name should be same as the name of your root directory where your settings.py file is located ))
Steps to push your code(project) into Heroku
a. git init
b. git add .
c. git status
d. git commit -m “First Commit”
e. heroku create appname (give your own appname)
f. heroku open command is used to open your app in the browser
g. git push heroku master command is used to push code into heroku
Go to settings.py file in your project and then add your app domain name in ALLOWED HOSTS
ALLOWED HOSTS = [‘your app domain name’]
Generate a new SECRET KEY which can be used for production. After generating new SECRET KEY update your environment variables file.
Next go back to the terminal and run the following commands
heroku config:set SECRET_KEY=”newgeneratedsecretkeyvalue”
heroku config:set DEBUG_VALUE=”TRUE”
Now update the settings.py file which is in your project directory and change the secret_key line to
SECRET_KEY = os.environ.get(‘SECRET_KEY“)
Commit the changes every time you make changes to the code
git add -A
git commit -m “Updated Commit”
git push heroku master
Creating Postgresql Database: Heroku recommends to use Postgresql Database
Heroku by default creates a postgres database when you create a heroku app.
If its not created then run the following command
heroku addons:create heroku-postgresql:hobby-dev
After creating database you need to make migration now using the below command
heroku run python manage.py migrate
Create Super User for your new database by using the following command
heroku run python manage.py create superuser
After creating superuser run the below command
Now the app should open in your browser. Check the entire functionality of your app and then finally change the DEBUG value in settings.py file in your project.
DEBUG = (os.environ.get(‘DEBUG_VALUE’ == ‘TRUE’)) and then finally commit the changes like how we did in Step 17 and now your web app is deployed on Heroku.
Hope this article helps!