Skip to content

Instantly share code, notes, and snippets.

@hdchinh
Forked from omushpapa/directions.md
Created July 18, 2021 09:00
Show Gist options
  • Save hdchinh/9c6d273c7a1d3551336bb5552acf8488 to your computer and use it in GitHub Desktop.
Save hdchinh/9c6d273c7a1d3551336bb5552acf8488 to your computer and use it in GitHub Desktop.
Deploy Django App on Heroku

Requirements

Run pip install pipenv to install pipenv

Run pipenv shell to create an environment, if does not exist, and activate it.

Run pipenv install python_decouple whitenoise dj_database_url Pillow gunicorn May take a while.

This should create two files: Pipfile and Pipfile.lock. Keep them in the project root.

Signup

Sign up on Heroku and install the Heroku Toolbeit

Run heroku login

Run heroku create <app name>

Add Heroku required files

Add a Procfile file in the project root with the following content

web: gunicorn <project name>.wsgi --log-file -

Run pip freeze >> requirements.txt

Configure static files

In settings.py, set up as follows:

With PROJECT_ROOT as

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

Configure STATIC_ROOT, STATIC_URL and STATICFILES_STORAGE as

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

In wsgi.py, set up as follows:

import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bootcamp.settings")

application = DjangoWhiteNoise(get_wsgi_application())

Configure Media files

In settings.py, add

# Media
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')
MEDIA_URL = '/media/'

Database

Add a database to your Heroku app Run heroku addons:create heroku-postgresql:hobby-dev

Update settings.py with

import dj_database_url       # Place this line preferably at the top
from decouple import config  # Place this line preferably at the top

SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', default=False, cast=bool)
DATABASES = {
    'default': dj_database_url.config(
        default=config('DATABASE_URL')
    )
}

Ensure you replace: a) the initial DATABASES configuration as appropriate, NOT adding b) the initial DEBUG configuration c) the initial SECRET_KEY configuration. Copy this somewhere temporarily.

These values tend to be different in the development and production environments hence their replacement.

Create a settings.ini file in the project root and add the following (replace appropriately):

[settings]
DEBUG=True
SECRET_KEY=<your app SECRET_KEY in settings.py>
DATABASE_URL=[database type]://[username]:[password]@[host]:[port]/[database name]
# DATABASE_URL=postgres://[username]:[password]@localhost:5432/[database name]

Create a .gitignore file and add the following line:

settings.ini

We don't need these file in the production environment.

Config vars

Head over to the Heroku Dashboard and check into you recently created app.

Go to the Settings tab and click Reveal Config Vars

Add key SECRET_KEY with value as the SECRET_KEY you had copied somewhere.

Add key DEBUG with value True. This is for the moment. Ensure you change this once you go live

Commit file

In your project root, run

git add .
git commit -m "Initial commit"
git push heroku master

Run migrations

Run heroku run python manage.py migrate.

If the app crashes because of missing migrations (something I experienced), run

python manage.py makemigrations && python manage.py migrate
git add *migrations/*
git commit -m "Add migrations"
git push heroku master
heroku run python manage.py migrate

You are now good to go!

Thanks to Simple is Better than Complex. One of the very useful sites around! Thanks to Heroku Dev Centre Thanks to Google And my awesome efforts ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment