Running Test Build and Deploy with Heroku

Reference

Heroku Webhosting for Django - Python Django Dentist Website #12

Configuring Django Apps for Heroku

General

  • guincorn: Django uses “runserver” as the default package to run a server during development. However, when deploying with heroku, “gunicorn” should be used to run a server.

  • whitenoise: In local environment, django is capable of searching for static files by itself. In deployment, static files are to be manually collected using “whitenoise”.

  • procfile: Procfile defines what kind of app is being deployed and what server programs to use. In this case, a django app is a “web” app and uses “gunicorn” run the server

  • the Importance of requirements.txt: requirements.txt tells heroku which packages are required to run the app.

Django SECRET_KEY Separation (Windows)

When a django project is created with django-admin, a SECRET_KEY is automatically created in Settings.py. This key is used in various security features of django, and therefore should not be made public when the project is to be deployed. However, when working as a team, files might be shared through github. Thus, the key should be separated from Settings.py, and stored someplace else in the local environment. There are numerous ways to do this, but when deploying with heroku, the key is best stored as a environment variable.

First, the SECRET_KEY should be separated and be modified from

SECRET_KEY = <...SECRET_KEY...>

to

import os

SECRET_KEY = os.environ["SECRET_KEY"]

The second step is to set the environment variable in the Operating System. For Windows, this can be done in “Edit the system environment variable” menu.

When deployed with heroku, environment variables can be edited in the dashboard’s settings or using the CLI.

Errors encountered

Problem:

Although deployed without errors, the page shows only error messages.

Error Messages Found:

  1. using heroku log —tail
    1. status 503 “H10 App Crashed”
    2. during build: gunicorn → Worker failed to boot
  2. abnormalities during bulid git push heroku master
    1. Procfile declares types -> (none)

Cause of the Problem:

The directory levels of the git repository was set incorrectly, and thus, heroku could not read the Procfile at all, meaning that it did not know how to run the server. The critical clue was seen during the build.

Procfile declares types -> (none)

Searching for the reason for this error, I found the below question.

What is the reason for “Procfile declares types -> (none)” in Heroku?

Answer