Skip to content

Instantly share code, notes, and snippets.

@dwayne
Created August 12, 2012 13:20
Show Gist options
  • Save dwayne/3331788 to your computer and use it in GitHub Desktop.
Save dwayne/3331788 to your computer and use it in GitHub Desktop.
Setting up Django and Deploying to Heroku

Steps to get started with Django on Heroku

https://devcenter.heroku.com/articles/django

This quickstart will get you going with a Python/Django application that uses

  • a Postgres database
  • deployed to Heroku

Prerequisites

  • Heroku toolbelt
  • Python and Virtualenv
  • Postgres

Step 1 - Start a Django App Inside a Virtualenv

$ mkdir hello && cd hello
$ virtualenv venv --distribute
$ source venv/bin/activate

https://goshawknest.wordpress.com/2011/02/16/how-to-install-psycopg2-under-virtualenv/

$ sudo apt-get install libpq-dev python-dev
$ pip install django psycopg2 dj-database-url

Check that everything is setup correctly

$ django-admin.py startproject hello .
$ python manage.py runserver

If it is, then freeze the requirements

$ pip freeze > requirements.txt

Step 2 - Database Settings

Add the following to your settings.py:

import dj_database_url
DATABASES = { 'default': dj_database_url.config() }

Export the following environment variable with the following details (N.B. Can be put in venv/bin/activate since its already ignored by .gitignore)

export DATABASE_URL="postgres://user:pass@address:port/dbname"

Step 3 - Commit to Git

Contents of .gitignore

*.pyc
*.tar.gz
venv

Initial commit

$ git init
$ git add .
$ git commit -m "Initial commit"

Step 4 - Deploy to Heroku

$ heroku create
$ heroku apps:rename hello # if you want to change the random name that Heroku gives you initially
$ heroku keys:add # required on first push

$ git push heroku master && heroku open

Resources

Programatically determine the location of the Django project directory

import os

SITE_ROOT = os.path.dirname(__file__)

def get_root_path_to(directory):
    return os.path.join(SITE_ROOT, directory).replace('\\', '/')

Now, for e.g.

TEMPLATE_DIRS = (
    get_root_path_to('templates'),
)

Change the Time Zone

TIME_ZONE = 'America/Port_of_Spain'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment