Skip to content

Instantly share code, notes, and snippets.

@Jbot29
Last active July 5, 2016 08:35
Show Gist options
  • Select an option

  • Save Jbot29/ce6ed6abff04d0ae34e5 to your computer and use it in GitHub Desktop.

Select an option

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 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

#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

if os.environ.has_key('DATABASE_URL') and dj_database_url.config():
DATABASES['default'] = dj_database_url.config()

import os

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

STATIC_ROOT = 'staticfiles'

STATIC_URL = '/static/'

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

#Push code to Heroku

add all new files to git

git push heroku master

#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

#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

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