DEV Community

Cover image for Hosting a Python Django web application on Heroku
Tawanda Nyahuye
Tawanda Nyahuye

Posted on

Hosting a Python Django web application on Heroku

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 Django application from my GitHub.

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

Git
Heroku-cli
GitHub account
Heroku account

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

Installing django-heroku and gunicorn

pip install gunicorn
Enter fullscreen mode Exit fullscreen mode
pip install django-heroku
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

from os import environ
Enter fullscreen mode Exit fullscreen mode

Edit your secret key as follows:

Copy the current secret key and save it somewhere.

# 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!
Enter fullscreen mode Exit fullscreen mode
  • Heroku settings

Add the following import at the beginning of your settings.py:

import django_heroku
Enter fullscreen mode Exit fullscreen mode

Add the following at the end of your settings.py:

# Activate Django-Heroku.
django_heroku.settings(locals())
Enter fullscreen mode Exit fullscreen mode

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 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
Enter fullscreen mode Exit fullscreen mode

On Heroku

By now you should have set up your Heroku account

  • Creating a Heroku app, Click the New 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:

heroku login
heroku git:remote -a your-app-name
heroku run bash
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

After creating a superuser you can now use your app.

Congratulations your application is live check mine

Oldest comments (0)