Deploy a Django Project to Heroku from a GitHub Repository

Deploy a Django Project to Heroku from a GitHub Repository

So you have built a shiny new Django application that has the potential to change the world. It won't do much good if it's just sitting on your computer, you need to deploy it so that everyone in the world can have access to your beautiful brainchild. Here's how to deploy it straight from your GitHub repository to Heroku.

Preparation

First, make sure you have a requirements.txt file in your root directory (i.e. same directory with your manage.py file). This allows Heroku to recognize your application as a Python application.

Install gunicorn. Gunicorn is a Python HTTP server for WSGI applications like Django. It will help in deploying your project to Heroku. Simply run the command below to install it.

pip install gunicorn

If you have a virtual environment enabled (which you should, always use virtual environments), simply run the command below to create your requirements.txt file, otherwise, you'd have to create the file and write the requirements manually (see why you should use a virtual environment?)

pip freeze > requirements.txt

Next, create a file called runtime.txt and write the Python version you would like your application to use (it is advisable to use the same version you used while building it).

python-3.8.1

Lastly, create a file named Procfile. A Procfile simply specifies the command to be run at startup (in this case, it works with gunicorn).

web: gunicorn your_project_name.wsgi --log-file -

Handling Static Files

Heroku doesn't automatically handle static files for you, so you'll need some extra configuration to get them up and running. Whitenoise is a common library used to deploy static assets, but, I personally prefer to use dj-static.

Install dj-static with the command below.

pip install dj-static

Open your project's wsgi.py file and add the following lines of code.

from dj_static import Cling, MediaCling

application = Cling(MediaCling(get_wsgi_application()))

After this step, your wsgi.py file should look like this.

import os

from django.core.wsgi import get_wsgi_application
from dj_static import Cling, MediaCling



os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project_name.settings')

application = Cling(MediaCling(get_wsgi_application()))

MediaCling helps to handle user-uploaded content.

Next, make sure you have defined STATIC_ROOT and MEDIA_ROOT in your settings.py file.

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

Setting Up PostgreSQL

It is possible to use the default SQLite3 database which Django provides. However, you may want to use a production-ready database like MySQL or PostgreSQL.

To set up PostgreSQL, install the following libraries - psycopg2-binary and dj-database-url.

pip install pyscopg2-binary dj-database-url

Update your settings.py file with the following lines of code.

import dj_database_url
db = dj_database_url.config()

DATABASES['default'].update(db)

After completing these steps, run pip freeze > requirements.txt again to update the file with the new libraries you have installed, then push your project to your GitHub repository.

Setting up Heroku Application

Head over to Heroku, set up and account, connect it to your GitHub account, and create a new app.

After the app has been successfully created, select the Deploy tab and scroll down to the Deployment Method section, then, select GitHub.

image.png

Head over to your GitHub repository, and update your settings.py file with the following lines of code.

DEBUG = False
ALLOWED_HOSTS= ['your_domain.herokuapp.com']

This allows your Django application to recognize your Heroku domain as a valid host. Save the file and commit the changes.

Input the name of the repository into the search bar and click Search, then click the Connect button.

After connecting successfully, head over to the Manual deploy section and deploy your desired branch.

image.png

Running Commands using the Heroku CLI

If you opted for a fresh PostgreSQL database, you may notice that your superuser and database tables are unavailable. To run manage.py commands, you'll need to use the Heroku CLI.

Head over to the Heroku CLI download page and download the installer for your PC.

Open up a terminal and login to your Heroku account by running the command below.

heroku login

It'll prompt a browser login, simply click the button as instructed and you should be logged in to your Heroku account.

Next, migrate your database tables using the following command

heroku run python manage.py migrate -a your-app-name

Using the same approach, create a superuser, migrations for your app models, and run the manage.py migrate command to migrate those migrations.

Voila! You have successfully deployed your Django project to Heroku. You can enable automatic deploys so that whenever you push code to your repository, it auto-updates your application on Heroku.