Step-by-step guide for deploying a Django web app on Heroku with Git.

Django + PostgreSQL + Heroku + GIT

This tutorial is intended for beginners who want to deploy their Django web app on Heroku free of cost. It will be nothing fancy but a step by step guide which can be quickly referred to for the deployment. The prerequisite for this tutorial is the knowledge of Django, virtual environment in python, and git would be nice to have for understanding this better. If you are revisiting this blog, I’ll save you some time by clubbing all the commands in one place- find it at the end of this tutorial.

Login Heroku

In this section, we will do the project-specific configurations to make our project deployment ready. Here I’ll be using references to one of my project- TeamUp for explaining a few configurations on the go.

  • First of all, activate your python virtual environment and go to your directory in the project where the manage.py file is located.
  • Let’s start by creating a requirements.txt file. We need to specify the requirements or dependencies for our project in a requirements.txt file to help Heroku understand what all packages we need in order to get our project up and running. Follow the steps below:

Start by installing a few libraries using the below commands.

Now, you’ll see that a requirements.txt file is created in your current directory and it has all the libraries present in your virtual environment along with their versions.

  • Creating a Procfile (Make sure you spell it correctly and copy the below content of the Procfile carefully).

In the same directory where manage.py is present, create a file name as Procfile (it doesn’t have a file extension). It specifies the commands that are executed by the app on startup. Then paste the below content in Procfile and make sure that you change TeamUp in the second line to your projects root directory name (folder name in which your settings.py file is located).

  • Creating runtime.txt

In the same folder where you have your manage.py file create another file named runtime.txt wherein you write your python version like python-3.8.7 in my case.

runtime.txt

If you do not know which python version you are using then type the below command on the cmd/terminal and then copy the version and paste it in runtime.txt.

  • Changes in Settings.py file

Shown below are only the changes that we need to do in the settings.py file. Please note that you must be willing to use PostgreSQL DB that Heroku supports.

Settings.py File

Please read the inline detailed comments in the code above to know more about the changes that you have to make in the settings.py file. We are yet to set our Heroku website name so you may leave the ALLOWED_HOSTS blank for a while. Furthermore, in the above code, whatever is there in config() or os.environ.get() will have to be set on Heroku once we start actual deployment which will be the next step.

  • Install Heroku CLI on your system. Refer to the Heroku Toolbelt for the same.
  • Login to Heroku via Terminal/CMD

Type the command given below in the terminal to connect to the Heroku and enter your credentials to log in.

Heroku Login
  • Choosing a name for your website

Below is the command to create your app on Heroku via terminal. If the name you choose isn’t available then you may try some other name and fire the same command again. Do not forget to update the same app name in the ALLOWED_HOST list in settings.py

Website Name
  • Setting the configuration variables on Heroku

Remember we discussed setting the config() variables while making changes to our settings.py, this is the time we will do it. Type below given Heroku commands (starting with $, do not type $ though) on your terminal to set the config variables.

Set Config Variables

Once this is done, you will see it on the Heroku app using your browser.

Heroku Dashboard

Click on the “Reveal Config Vars” button in the settings tab of your application on Heroku Dashboard to see the variable allocation.

  • Initializing Git and pushing the code to Heroku

Alright enough configuration, lets’s push the code to Heroku. This is the final stage of the deployment. Pull the changes from your remote GIT repository if your project is not up to date. If you do not have git installed then go ahead and install it on your system.

Once you have your GIT set up and initialized using git init in your current directory, fire the commands given below on your terminal to stage the changes and push the code to Heroku on the master branch.

GIT

If your app is deployed successfully you'll see no errors and something like this at the end of your output on the terminal-

Successful Deployment

At this point, your app is deployed, yet we have to migrate the DB as well.

  • Migrate the database using the below command

Kudos! Now you’ll see that your web app is deployed successfully. Go to the website (yourappname.herokuapp.com) and you must see it live at this point.

You may discontinue from here on. However, we will discuss some optional things below to wrap up this tutorial.

For some reason, if the migrate and collect static doesn’t happen while deploying to Heroku then fire the commands given below on Terminal/Heroku CLI.

Other Heroku commands if needed

Finally, here is the LINK to all the steps we performed in this tutorial, in case you want to access it next time and save yourself some time than reading this blog again.

In my initial days of learning deployment, I struggled a lot as it was challenging to find a fully working Heroku deployment tutorial on the internet. So, after deploying multiple web apps I thought of sharing the steps that actually worked flawlessly. If you want to check out my projects on Django and Python do visit my GitHub profile. I hope this helps everyone learning to deploy a web app on Heroku.

Happy Deploying!

Computer Science Engineer- Data Science and Machine Learning Enthusiast.