Deploy Django App on Heroku for free
Deploy your django app on Heroku
To host your django app, first you have to install few pakages as shown below:
once gunicorn is installed, install the whitenoise dependency using the following command.
Now, create a file with the name as "Procfile" and do not add any extension to it. Open the file and paste the below code:
For Example, our project name is blog. So the code in procfile will be as shown below :
Now, we need to add a requirements.txt file. All the required dependencies will be mentioned in that file for Heroku to know the requirements of our project. To create this file, just paste the following command in your gitbash/ command prompt.
You can open the requirements.txt file and you will find all the requirements and dependencies required to run your django app.
Open the settings.py file and in the ALLOWED_HOSTS just add the domain name/appname which you have created in heroku. This will allow that domain to access our project. But your project will not run on the localhost now. To run it on the localhost, just add the localhost in the list as shown below.
For Example, our heroku appname is blogom, when you click the view app in heroku, it will open a new tab in the browser with the address blogom.herokuapp.com. Mention that domain name in the allowed host list in the :
You can use either 127.0.0.1 or you can just write "localhost" to run your django app on the localhost.
Note: When deploying your django app, set the debug to false.
Add the whitenoisemiddleware after the SecurityMiddleware and before the SessionMiddleware as shown below:
When deploying to heroku, you will get an error that heroku can't get staticfiles. To remove that error, just create a folder in your main directory and name it staticfiles. Open your settings.py file and add the below code :
And now our django app ready to deploy. Watch the above video for step by step demonstration.