Skip to content

Instantly share code, notes, and snippets.

@jmcvetta
Forked from trey/django_2012.md
Created July 2, 2012 03:15
Show Gist options
  • Save jmcvetta/3030785 to your computer and use it in GitHub Desktop.
Save jmcvetta/3030785 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

Develop on Ubuntu; Source on Github; Deploy to Heroku


Install system packages

You may already have (some of) these installed. It is best not to install Django system-wide, to avoid confusion with the Django instance that will be installed inside your virtual environment below.

$ sudo apt-get install python python-pip python-virutalenv git
# Install Heroku via PPA
$ wget -qO- https://toolbelt.heroku.com/install.sh | sh

Create a new repository on Github

Create a new repo the usual way. Check the "Initialize this repository with a README" box and choose a "Django" .gitignore file from the list.

Clone the repo to your local computer.

$ git clone git@github.com:jmcvetta/myproject.git

Create the project

Setup and activate virtualenv.

$ cd myproject
$ virtualenv --no-site-packages venv
$ source venv/bin/activate

(venv for virtualenv. That is the conventional name, but you can choose whatever you like.)

Install some things into your virtualenv.

$ pip install Django psycopg2 south pillow dj_database_url ipython gunicorn django-bcrypt
  • psycopg2 is a PostgreSQL adapter for Python.
  • South handles database migrations.
  • Pillow is a virtualenv-safe replacement for PIL.
  • dj_database_url makes DB settings for Heroku easy.
  • IPython provides an enhanced Python shell for your development pleasure.
  • Gunicorn 'Green Unicorn' is a Python WSGI HTTP Server for UNIX.
  • django-bcrypt provides more secure password encryption using bcrypt.

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

$ pip freeze > requirements.txt

Start a Django project

$ django-admin.py startproject myproject .

Add south and gunicorn to your INSTALLED_APPS in settings.py.

INSTALLED_APPS = (
    ...
    'south',
    'gunicorn',
    'django_bcrypt',
)

Configure settings to use Heroku's Postgres database when deployed, and SQLite locally.

import dj_database_url
import os
DATABASES = {'default': dj_database_url.config(default='sqlite:///' +
        os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'db.sqlite') )}

Create a Procfile to to launch the web process using Gunicorn.

# Note the single quote marks so $PORT gets echoed literally
$ echo 'web: gunicorn myproject.wsgi -b 0.0.0.0:$PORT' > Procfile

Go make something awesome!

Deploy to Heroku

$ 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. $ python ./manage.py schemamigration [appname] --auto
  3. $ python ./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 venv/bin/activate

Install new packages with Pip, then update requirements.txt file.

$ pip install django-nose
$ pip freeze > requirements.txt
$ git commit requirements.txt -m "Added django-nose to requirements.txt"

If requirements.txt was updated elsewhere, you can update your virtualenv:

$ pip install -r requirements.txt

Sync and/or migrate your database:

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

Finally, fire up your server:

$ python ./manage.py runserver

Sources

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