Hosting a Python Django web application on Heroku
Tags: python, django, hosting, heroku
Published: 2020-07-20 13:20:11

In this tutorial, we will host a web application developed with Python Django framework on Heroku. We are going to assume that you already have the knowledge to build an application using Django, your only problem is deploying your app online. I am using my [bus-routing](https://github.com/Towernter/Django-bus-routing) Django application from my GitHub. 

Before following this tutorial you need to set up the following tools:

[Git](https://git-scm.com/downloads)
[Heroku-cli](https://devcenter.heroku.com/articles/heroku-cli)
[GitHub account](https://github.com/join)
[Heroku account](https://signup.heroku.com/)

At this point, you should have your Django project running in your virtual environment on your local machine.

#### Installing [django-heroku](https://devcenter.heroku.com/articles/django-app-configuration) and [gunicorn](https://gunicorn.org/) 
```
pip install gunicorn
``` 
```
pip install django-heroku
```

#### Setting up your `requirements.txt`
We are not going to use your virtual environment therefore we need to create a file with a list of requirements(all the packages installed in our project). Heroku will use this file to install the packages for our project.

Inside your virtual environment run the following command:
```
pip freeze> requirements.txt
```
On completion, a text file named `requirements.txt` should be created with a list of all the packages your project is using.

#### Creating a `Procfile`
Create a new file in your virtual environment in the same folder with the `requirements.txt` and name it `Procfile`(don't give it a file extension).

>_We will use the Procfile to scale up our dynos_

Inside the Procfile enter the following `web: gunicorn your-project-name.wsgi` and save it.

#### Setting up your `settings.py` for Heroku deployment
* Hiding your secret key

We need to hide your secret key in case you make your GitHub repository public so that no one can see it. Add the following import in your `settings.py` 
```python
from os import environ
```
Edit your secret key as follows:

_Copy the current secret key and save it somewhere_.
```python
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = environ['secret_key']
# SECURITY WARNING: don't run with debug turned on in production!
```
* Heroku settings

Add the following import at the beginning of your `settings.py`:
```python
import django_heroku
```
Add the following at the end of your `settings.py`:
```python
# Activate Django-Heroku.
django_heroku.settings(locals())
```
#### Pushing your project to GitHub
Get rid of your virtual environment or save your Django project in a separate folder(we do not want to use the local virtual environment Heroku will create its own environment using your `requirements.txt`). Create your GitHub repository and push your project as follows:
```git
git init
git remote add origin git@github.com:your-name/repository-name.git
git add .
git commit -m "first commit"
git push -u origin master
```
#### On Heroku
By now you should have set up your [Heroku](https://signup.heroku.com/) account
 
* Creating a Heroku app, Click the [New](https://dashboard.heroku.com/new-app) button on your Heroku account to create a new App
* Choose a unique name for your App and set the country to whatever you like
* Setting your secret key: We haven't forgotten about your hidden secret key. Click on settings and below your application details click on `Reveal config vars`. `key` is `secret_key` and value paste your saved secret key.
* Go to the `deploy` option
* Click on GitHub `connect to GitHub`
* Enter the repository name that you created earlier and search
* Connect your GitHub repository by clicking on `connect`
* Deploy Branch(it should be set to master) enabling automatic deploys will automatically deploy changes you make to your GitHub repository
* Now you should see Your app was successfully deployed.
* Click on View(You can view but you can't log in)

#### Creating database tables and a superuser
*On your CLI inside our project folder follow the following steps:
```git
heroku login
heroku git:remote -a your-app-name
heroku run bash
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
```
>After creating a superuser you can now use your app.

####Congratulations your application is live [check mine](https://bus-routing.herokuapp.com/)