Deploying Django Apps on Heroku

Aakash Verma
Analytics Vidhya
Published in
4 min readSep 9, 2020

Hi Readers , I trust you are doing good . I am up again with another article on deploying Django websites on Heroku . Deployment is a very important part of website and can be very tiresome if not proceeded in careful manner . In this article we will learn to deploy a django website on Heroku So let’s start.

I will be deploying a todo_list site I have created .Either You can use your own site or can clone my todo_list project . Here is the Link

Installing Virtual Environment: -

virtualenv is a tool to create isolated Python environments to have our project configuration isolated from the system and other projects as well . You can install it using below command in windows .

pip install virtualenv

Installing git: — git is the version control system that is required to handle small and large projects with speed and efficiency . we will require it to push the code to heroku .

Download git in windows using this link

after download check if it is properly installed using following command in command prompt.

git --version
git version 1.7.7.3 #would return something like this

Now to deploy our application on heroku we need a heroku account ,

let’s create one, go to link and create yourself a new heroku account and install heroku CLI as well .

Login heroku CLI

Once heroku CLI has been installed go to your terminal or command prompt and run

heroku login

above command will prompt you for your heroku credentials like email and password and will let you login into your heroku profile .

Now go to your project root directory and copy the project to a different directory in your system and create a virtual environment around it using virtualenv command below

python -m venv env #create virtualenv env
env\Scripts\activate #activate virtualenv env

Or if you have cloned the project from github link , then also create a virtual environment around it as well .

Installing other packages or libraries , If you have other packages like requests, Pillow etc, install it using following command

pip install package_name

Once all packages are installed we have to update purr requirements.txt file

run following command in terminal ,

pip freeze>requirements.txt

This is will add updated requirements.txt file in our root project folder with package names , as this file will be used by heroku to identify package requirements .

Now run

python manage.py runserver 

This would run our project in local envrionment successfully , If you face any error saying no table present in database , this may be due to migrations, you can either run migrations here or directly in heroku bash which we will do later, I recommend doing it later as to avoid some conflicts .

Making a Procfile :- This file is used to explicitly declare your application’s process types and entry points. It must be located in the root of your repository or project folder .

In Your root project folder , make a file named Procfile(exactly same name)

Procfileweb: gunicorn myproject_name.wsgi 

Installing gunicorn : — This improves our deployment process and makes it efficient fast . Use following command to install gunicorn

pip install gunicorn

Installing django-heroku: — The django-heroku package automatically configures your Django application to work on Heroku.

#installing django-heroku
pip install django-heroku

Adding django_heroku to settings.py file

#settings.pyimport django_heroku # at the top...
...
..
# Activate Django-Heroku.
django_heroku.settings(locals()) #at bottom of settings.py file

Creating heroku app

heroku create app_name ## creating heroku app ....#Terminal will be seen with something like this 
Creating app... done, ⬢ app_name
https://git.heroku.com/app_name.git

once above thing is completed we need to add a remote git repo to our heroku app, we can do it using following command .

heroku git:remote -a app_name 
#Terminal will be seen with something like this
set git remote heroku to https://git.heroku.com/app_name.git

adding your code files to remote github repository ,

You can use .gitnore file to automatically remove unwanted files from staging area

git status  #to check which files have been staged 
git add -A # to add all code files at once
git commit -m "initial message" # to commit code files to remote repository .

After commit of the code files is done , push the code ,Use following command

git push heroku master
#Terminal will be seen with something like this
...
...
Initializing repository, done.
updating 'refs/heads/master'
...
once this is complete you can got to the your app link generated by heroku and see your site live.

If there is no user table or session table error in your site, that is because we haven’t migrated our database , you need to migrate the database .

In command line run

heroku run bash 

This will open a python runtime like we have in local django orm, use this to migrate the model files to database.

python manage.py makemigrations app_name 
python manage.py migrate

now go to your site , This must be done and you would see your site live

Thanks for reading this article and I hope you have found it useful, let me know in the comment section and give it a clap if you like it . Happy coding.

--

--

Aakash Verma
Analytics Vidhya

Computer Science Undergrad ,Tech Enthusiast , Loves writing about Technology