Skip to content

Instantly share code, notes, and snippets.

@tedtieken
Forked from trey/django_2012.md
Created August 5, 2012 18:17
Show Gist options
  • Select an option

  • Save tedtieken/3266518 to your computer and use it in GitHub Desktop.

Select an option

Save tedtieken/3266518 to your computer and use it in GitHub Desktop.
How to start a Django project in 2012

How to start a Django project in 2012

(and deploy to Heroku)

First, warm up system.

$ easy_install pip
$ pip install virtualenv
$ pip install django
$ gem install heroku
$ brew install postgresql

Start a project.

$ django-admin.py startproject [myproject]

Setup virtualenv and start it up. put in home directory, because I use VirtualBox and there are symlink issues if I put it on the share drive with the rest of the code

$ virtualenv --no-site-packages --distribute ~/ve/[ve-name]
$ touch virtenv_is_[ve-name]
$ source ~/ve/[ve-name]/bin/activate

Install some things into your virtualenv.

$ pip install Django psycopg2 south dj-database-url

Now load the exact version into a requirements.txt file.

$ pip freeze > requirements.txt

(psycopg2 is a PostgreSQL adapter for Python. mysql-python is a MySQL adapter for Python)

Put south in your INSTALLED_APPS in settings.py.

INSTALLED_APPS = (
    'south',
    ...

Make .manage.py executable (so you don't have to type python manage.py ... all the time).

$ chmod +x manage.py

Don't forget to add ve to your .gitignore file.

Database settings for local development and Heroku

Create a database for local use.

sudo -u postgres createdb [whatever]

Add the following to settings.py:

import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost/[whatever]')}
#postgres://username:password@host:port/database_name

Add the following to local_settings.py:

import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://username:password@localhost/[whatever]')}

Now go make something awesome.

Deploy to Heroku

$ git init
Initialized empty Git repository in /Users/kreitz/hellodjango/.git/
$ git add .
$ git commit -m "my django app"
$ heroku create
$ git push heroku master
$ heroku run ./manage.py syncdb
$ heroku open

Update your database with South and push the changes to Heroku

  1. Make changes to your models
  2. $ ./manage.py schemamigration [appname] --auto
  3. $ ./manage.py migrate [appname]
  4. [commit & push changes to heroku]
  5. $ heroku run ./manage.py migrate [appname]

Working on your project later

Whenever you work on your project, you'll want to activate your virtualenv:

$ source ve/bin/activate

Then load any new requirements:

$ pip install -r requirements.txt

Sync and/or migrate your database:

$ ./manage.py syncdb
$ ./manage.py migrate [appname]

Finally, fire up your server:

$ ./manage.py runserver 0.0.0.0:8000

Sources

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