Deploying a Django project on Heroku

1. Modify settings.py

1-1. Debug

DEBUG = bool( os.environ.get('DJANGO_DEBUG', True))

1-2. SECRET_KEY

import os 
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'YOUR_SECRET_KEY')

2. Install Heroku

npm install -g heroku

3. Update the app for Heroku

3-1. Procfile

web: gunicorn [YOUR_APP_NAME].wsgi --log-file -
  • same directory as manage.py

3-2. Install Gunicorn

The Gunicorn "Green Unicorn" is a Python Web Server Gateway Interface HTTP server.

pip install gunicorn

3-3. Database configuration

dj-database-url (Django database configuration from environment variable)

pip install dj-database-url 

Add it into the bottom of the settings.py

# Heroku: Update database configuration from $DATABASE_URL.
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

psycopg2 (Python Postgres database support)

pip install psycopg2-binary

3-4. Serving static files in production

Install whitenoise

pip install whitenoise

Add it into the MIDDLEWEAR of the settings.py

MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

Add it into the bottom of the settings.py

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_URL = '/static/'

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

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

3-5. Requirements

The Python requirements of your web application must be stored in a file requirements.txt in the root of your repository

pip freeze > requirements.txt

3-6. Runtime

runtime.txt tells Heroku which programming language to use

python-3.6.9

4. Create and upload the website

4-1. Create the app

heroku create [APP_NAME]

4-2. Push our app to the Heroku repository

git add .
git commit
git push heroku master

4-3. Set up the database tables

heroku run python manage.py migrate

4-4. Create superuser

heroku run python manage.py createsuperuser

4-5. Open your app

heroku open

5. You are now live

+

Heroku Tips & Tricks

1. Disable collectstatic

heroku config:set DISABLE_COLLECTSTATIC=1

To export the data from your Heroku Postgres database, create a new backup and download it.

heroku pg:backups:capture
heroku pg:backups:download

3. Maintenance Mode

To enable maintenance mode

$ heroku maintenance:on
Enabling maintenance mode for myapp... done

To disable maintenance mode

$ heroku maintenance:off
Disabling maintenance mode for myapp... done

To check the current maintenance status of an app

$ heroku maintenance
off

4. Restart

heroku restart
  • run this when you see this message

    • "Your account has reached its concurrent builds limit"

5. Loaddata

heroku run python manage.py loaddata [YOUR_JSON_FILE_NAME]

Last updated