Procedure for deploying a Django application using Heroku
Step 1: First of all you are required to execute the command as shown 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.
Step 2: Now, go to your Django project and open ‘settings.py’ as shown in the image below.
Step 3: At the end of ‘settings.py’ add the following statement.
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.
Step 4: A Procfile is essential in the Heroku application. Therefore, requires a ‘nano’ command is used to edit/create the ProcFile. Hence, first of all, you are required to execute the following command.
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.
Step 5: Now, after you have successfully created the ProcFile, open the given file in the text editor. Then, you are required to update ‘web: gunicorn myproject.wsgi’ to ‘web: gunicorn weatherapp.wsgi’. The updated statement that is to be updated in the ProcFile is provided below.
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.
Step 6: Now, you are required to install Gunicorn.
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
Step 7: Here, you are required to install ‘django-heroku’ package. So, in order to install the package, you need to run the following command.
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
Step 8: Now, you need to import ‘os’ and ‘django-heroku’ package in setting.py as shown in the image below. The code for importing the packages has also been provided below.
import os import django_heroku
Step 9: Now, to activate ‘django_heroku’ you are required to go to the end of ‘settings.py’ and add in the following lines of code.
# 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.
Step 10: Since, you are now moving onto the production site, the ‘DEBUG’ in ‘settings.py’ should be set as ‘FALSE’ 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.