Sitemap
Samuels

Samuels

Heroku Configuration

2 min readMay 9, 2023

Deploying a Django web application to Heroku can be a simple and straightforward process with the right steps. In this article, we’ll walk through the necessary steps to configure a Django app for Heroku and deploy it to a live server. By the end, you’ll have a fully functional Django app running online and accessible to anyone with an internet connection.

To get started, you’ll need to have a Heroku account set up and installed first. Our deployment checklist contains five steps, including installing Gunicorn, creating a requirements.txt file, creating a Procfile and runtime.txt file, and updating ALLOWED_HOSTS in django_project/settings.py.

Once you’ve completed these steps, you’ll be ready to add and commit your changes to Git and push them up to GitHub. From there, it’s a simple matter of logging into your Heroku account, running the create command, and pushing your code to Heroku to start your deployment. Don’t forget to add free scaling so your app is available online!

Whether you’re a seasoned developer or just getting started with Django, this guide will give you everything you need to know to deploy your app to Heroku with confidence. Let’s get started!

Have a Heroku account set up and installed first.

Our deployment checklist contains five steps:

  • install Gunicorn with Pip i.e,

(.venv) > python -m pip install gunicorn==20.1.0

  • create a requirements.txt file ,output the contents of our virtual environment to a requirements.txt file. i.e,

(.venv) > python -m pip freeze > requirements.txt

  • create a Procfile
  • create a runtime.txt file
  • update ALLOWED_HOSTS in django_project/settings.py

The two local hosts Django runs on are localhost:8000 and 127.0.0.1:8000 and any Heroku site will end with .herokuapp.com which means we should add all three hosts to our ALLOWED_HOSTS configuration.

Code

# django_project/settings.py

Get Samuels’s stories in your inbox

Join Medium for free to get updates from this writer.

ALLOWED_HOSTS = [“.herokuapp.com”, “localhost”, “127.0.0.1”]

Procfile

web: gunicorn django_project.wsgi — log-file —

runtime.txt

python-3.10.2

Add and commit our new changes to git and then push them up to GitHub. (.venv) > git status

(.venv) > git add -A

(.venv) > git commit -m “New updates for Heroku deployment”

(.venv) > git push -u origin main

Heroku Deployment

Login to your account on heroku

(.venv) > heroku login

Then run the create command and Heroku will randomly generate an app name.

(.venv) > heroku create Creating app… done,

(.venv) > heroku config:set DISABLE_COLLECTSTATIC=1

Push the code to Heroku and add free scaling so it’s actually running online.

(.venv) > git push heroku main

--

--