Deploying Django Application using Heroku — Explained in detail
After you have developed a Django application, your next will be to make your application live and available for the users. There are various platforms through which you can deploy your application. But in this blog, I will be focusing on deploying a django application using Heroku platform.
Before you dive into deploying django application using Heroku, you should set up a virtual environment for your Django project. Also, make sure to activate the virtual environment. But if you require guidance for configuring your virtual environment, you can view my previous blog regarding the virtual environment which has been provided below-
Furthermore, in this blog files are managed using Heroku CLI as it allows us to create and manage Heroku applications efficiently. Hence, if you have not installed Heroku CLI yet, then you can have a look at my previous blog regarding Heroku and the installation of Heroku which has been provided below-
Also, it can be handy if you have acquired knowledge regarding shell commands as we will be deploying the application using CLI (Command Line Interface). So, if you are willing to acquire knowledge regarding some of the basic shell commands then you can view the blog provided below-
pip freeze > requirements.txt
‘pip freeze’ command is used to show all the installed files. Likewise, ‘pip freeze > requirements.txt’ copies the name of all the files to a text file which is named as ‘requirements.txt’.
Then, you can execute the command as ‘cat requirements.txt’ for viewing the contents that are present in the ‘requirements.txt’ file.
cat requirements.txt
STATIC_ROOT = os.path.join(BASE_DIR, ‘static’)
Django is unable to automatically create a target directory i.e. STATIC_ROOT. Hence, in ‘settings.py’, a variable called ‘STATIC_ROOT’ is responsible for defining the single folder where you are willing to collect all your static files.
Note: Before you carry out the next steps, make sure that you have installed Heroku CLI in your PC and have developed an account at Heroku. For ensuring that Heroku has been correctly installed in the PC, execute the following command, and make sure that you obtain the output as shown in the image below.
heroku -h
nano ProcFile
After the files open, provide the following statement in the file.
web: gunicorn myproject.wsgi
Here, the created ProcFile requires a Gunicorn as it is one of the most preferred production web server for Django applications.
After you have provided the statement, press ‘Ctrl+X’ then ‘Enter’ to save the changes made in the ProcFile.
web: gunicorn weatherapp.wsgi
Here, myproject.wsgi is updated as weatherapp.wsgi using a text editor (Sublime text) as shown in the image below.
Note: If you want to learn more about Web Server Gateway Interface (WSGI) in detail, please visit the link provided below.
Hence, at first, you are required to execute the following queries before carrying out further tasks.
pip install gunicorn
‘pip install gunicorn’ is used to install Gunicorn for your project. Finally, now you can execute the following commands.
pip freeze > requirements.txtcat requirements.txt
pip install django-heroku
Django application should be configured in order to work on Heroku. Hence, ‘django-heroku’ package is able to carry out the configurations part automatically which allows your Django application to work Heroku.
Then, you can execute the following commands.
pip freeze > requirements.txtcat requirements.txt
import os
import django_heroku
# Activate Django-Heroku.
django_heroku.settings(locals())
Note: Here, in this blog, we are not using our GitHub repository with the application which is going to be deployed in Heroku. Therefore, if you have cloned your application from your remote repository, move your application into a fresh new folder as shown in the image below.
DEBUG = False
Let us obtain an understanding of — Why do we need to set DEBUG as False?
First of all, let us understand what happens when DEBUG is set as True. DEBUG = True keeps details regarding the error pages. Hence, Django provides a stacktrace of what had gone wrong which can be quite helpful when you are debugging your application. Furthermore, Django also keeps track of all the SQL queries that had been executed in DEBUG mode.
Now, let on move onto the outcome when DEBUG is set as False. Keeping the DEBUG mode as True keeps track of all the SQL queries which had been executed in DEBUG mode which is not convenient in the production site. Hence, you are now required to provide ALLOWED_HOSTS in the settings.py file which identifies where you are hosting your Django application.
Note: If you require guidance for configuring ALLOWED_HOSTS, you can go to step 23 of this blog. Also, you should have created an app in Heroku which has been carried out in step 18 before you configure the ALLOWED_HOSTS.
Step 11: Now, go to your file explorer and right-click, then select the ‘Git Bash Here’ option as shown in the image below.
Note: If you have not installed Git Bash yet and require guidance for installing Git bash then you can view my previous blog which consists of a detailed procedure for installing Git.
git init
git add .
git commit -m “deploy”
Note: Before carrying out the following procedure, make sure that you have Heroku CLI installed on your PC. For further guidance, you can view my installation blog regarding Heroku CLI.
Step 15: So, to login to Heroku, you are required to provide the following command.
heroku login
heroku create app_name
Note: Here, in the above command ‘app_name’ represents the name that provided for your application which will be acting as a subdomain of Heroku.
Step 19: After the successful execution of the command you can now view your application in Heroku as shown in the image below.
git push heroku master
The above command is used to push the code from your master or main branch of the local repository to the Heroku remote.
heroku run python manage.py migrate
The database which is present in Heroku and the database which is present in your local machine is not the same one. Hence, if you add/create a user, or add various types of information in your local environment, it won’t be presented in the Heroku. Therefore, you should execute all the queries of the local environment in the production.
heroku open
Note: However if you have not configured your ALLOWED_HOSTS, your application will not be deployed in Heroku. Hence, to configure your ‘ALLOWED_HOSTS’ go to the ‘settings.py’ and find ALLOWED_HOSTS. Then, you are required to carry out the following steps.
ALLOWED_HOSTS = [‘https://deploy-weather-application.herokuapp.com',
‘localhost’,
‘127.0.0.1’]
Now, a question arises why is it required to provide the ALLOWED_HOSTS. Actually, by providing ALLOWED_HOSTS, it prevents the web application from HTTP Host Header attacks.
In ALLOWED_HOSTS, a URL is provided in which the Django application is going to be launched. For example https://deploy-weather-application.herokuapp.com. It is the URL in which the application is going to be hosted. Here, your URL is defined as per your application name i.e. https then the name which you have provided while creating your Heroku application, for instance, deploy-weather-application and finally the subdomain of Heroku i.e. herokuapp.com.
Also, you can obtain the URL of your application while creating the application as shown in the image below.
In the above case scenario of ALLOWED_HOSTS, the application can also run in the localhost. Here, 127.0.0.1 refers to the loopback Internet protocol (IP) address which is also known as the localhost.
Note: You are required to make changes inside the ‘setting.py’ file in ‘ALLOWED_HOSTS’ as shown in the image below.
git add .
git commit -m “edit”
git push heroku master
heroku open
https://devcenter.heroku.com/articles/django-app-configuration
https://help.heroku.com/GDQ74SU2/django-migrations
https://devcenter.heroku.com/articles/heroku-cli
I am Hrishav Tandukar, currently working as an Assistant Lecturer and Senior Student Project Supervisor concurrently at Islington College. Likewise, I have been selected as an AWS Cloud Ambassador of Cohort 2020. Talking about my interest, I am an AI and Cloud Computing enthusiast.
Thanks for this step-by-step guide! It really summarizes some of the most important topics on machine learning.