Skip to content

Instantly share code, notes, and snippets.

@Jbot29
Last active July 5, 2016 08:35
Show Gist options
  • Save Jbot29/ce6ed6abff04d0ae34e5 to your computer and use it in GitHub Desktop.
Save Jbot29/ce6ed6abff04d0ae34e5 to your computer and use it in GitHub Desktop.
Django Deploy to Heroku

#Create new git repo

At the same level as manage.py

#Create Heroku account

Go to http://heroku.com and get a free account.

#Install Heroku Toolbelt

Download the heroku toolbelt and install:

https://toolbelt.heroku.com/

Run the package file.

#Create Virtual Environment

In the directory where manage.py lives run:

sudo pip install virtualenv

virtualenv venv

source venv/bin/activate

pip install django

pip install gunicorn

pip install dj-database-url

pip install whitenoise

pip install psycopg2

Pip install any modules that you need - Examples requests BeautifulSoup

#Create Procfile

Create a file called Procfile with upper case P in the same directory with manage.py

put the line below in that file

web: gunicorn django project name.wsgi --log-file -

#Install all pip requirements

Install any pip modules you need

pip freeze > requirements.txt

add

psycopg2==2.6.1

to requirements doc

#Settings changes

Update the /settings.py file based on the link below under Django Settings

Place at the bottom of settings.py

import dj_database_url

db_from_env = dj_database_url.config(conn_max_age=500)

DATABASES['default'].update(db_from_env)

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))

STATIC_URL = '/static/'

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

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

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

Notes: https://devcenter.heroku.com/articles/getting-started-with-django#django-settings

#Update wsgi

Update /wsgi.py per the instructions

from django.core.wsgi import get_wsgi_application

from whitenoise.django import DjangoWhiteNoise

application = get_wsgi_application()

application = DjangoWhiteNoise(application)

#Create new Heroku app

heroku create

#Setting Environment variables

Environment variables locally

Using it in Django settings.py

in settings.py - using env var

import os

SECRET_KEY = os.environ.get('SECRET_KEY')

Setting environment var in the shell/terminal

export SECRET_KEY=myawesomekey

env

#Setting Enviornement Variables on Heroku

heroku config:set SECRET_KEY=8N029N81

S3_SECRET=9s83109d3+583493 ...

Notes: https://devcenter.heroku.com/articles/config-vars

#Push code to Heroku

add all new files to git

git push heroku master

#Run Migration on heroku

First add basic postgres on heroku site

heroku run python manage.py migrate

#Create admin user

create an admin super user

heroku run python manage.py createsuperuser

#Create new secret key (only if secret was pushed to a public git hub)

http://www.miniwebtool.com/django-secret-key-generator/

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