Deploying Python and Django Apps on Heroku

Deploying Python and Django Apps on Heroku

In this tutorial, we will learn how to deploy Django applications on Heroku.
Run the following command to install the dependencies we need.

$ pipenv install psycopg2 dj-database-url whitenoise gunicorn

Open the file and make the following changes.

import os
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'some_very_secret_key')
DEBUG = os.environ.get('DJANGO_DEBUG', '') != 'False'
ALLOWED_HOSTS = [''] # change this to your application host
    .... ] import dj_database_url prod_db = dj_database_url.config(conn_max_age=500) DATABASES['default'].update(prod_db)
STATIC_URL = '/static/'
    BASE_DIR / 'static'
STATIC_ROOT = BASE_DIR / 'static_root'

Now that we have made our changes, it's time to deploy.
In the root of the project, create a file called Procfile, and add the following:

release: python3 migrate
web: gunicorn config.wsgi --log-file -
In your Heroku dashboard, create a new application.
Open a terminal window and type the following before we proceed with the deployment.

$ heroku config:set DISABLE_COLLECTSTATIC=1 --app Your_Heroku_App_Name_Here
$ heroku config:set DJANGO_DEBUG=False --app Your_Heroku_App_Name_Here
$ heroku config:set DJANGO_SECRET_KEY='YOUR_PROD_SECRET_KEY_HERE' --app Your_Heroku_App_Name_Here

We will deploy to Heroku through GitHub, so go to your Heroku dashboard, and select your application and click on Deploy tab, in the Deployment method, select GitHub.
Search for the repository and click Connect.
There are two options to make deployments from GitHub, Automatic deploys or Manual deploys.
For now, we can deploy the application manually by clicking on the Deploy Branch button.
That's it, we successfully deployed our Django application on Heroku.

Download source code: GitHub

Share this: