December 26, 2010

Django Boilerplate

I've made quite a few Django projects now.  Two that are published are Martlet.ca and Orange Rhyme Photography, but there are many more where that came from. As a result, I've made some observations about things I do when I start such a project. Here are the steps I take.

Deploy Virtualenv

Virtualenv is a must for people with more than one Python project on their development machine.  It's invaluable to be able to isolate python libraries depending on your project, if only so that you can keep track of which libraries are and are not installed for when you deploy.  I would go so far as to say that Virtualenv is the only python package that you should have installed systemwide.

A quick "virtualenv --no-site-packages <project_dir>" is all it takes for me to start a project. Then, "source bin/activate" to activate it and control your python paths.  Any python packages you install from here on go into the virtual environment.

Essential Packages

Every project has different needs.  But, every Django project has at least one need in common, and that is that Django be installed. Other packages I always like to have:

  • django-annoying: Really makes things a lot nicer. Read more at https://bitbucket.org/offline/django-annoying
  • south: I don't know why Django doesn't include migrations, especially given some of the stuff that is inbuilt, but South meets this need and then some. http://south.aeracode.org/
  • markdown: It's a stretch to call this essential, but somehow it always comes up. Use it for flatpages.

Media and templates:

Within the virtual environment, I usually have the following tree structure:

bin/
lib/
include/
site/ 
  <project name>/
  db/ 
  media/
  templates/
    base.html
    core/

Within the the templates directory, I create a base.html file based off of HTML5 Boilerplate, the last word in HTML starter templates (and the basis of the title of this post). Within the media directory I put the js, css, favicon, apple-touch-icon, and robots.txt files demanded by HTML5 boilerplate. This keeps everything nice and separate, yet consistant.

Django Settings

The only projects that need one settings.py are ones that will never be deployed.  Therefore, my first action within the django directory (after using `django-admin startproject`) is to create a settings directory and a base settings file.  Then, I create a development settings file to drop over that one, like so:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
from base import *
 
DEBUG = True
TEMPLATE_DEBUG = DEBUG
 
PROJECT_DIR = "/path/to/project/directory/"
 
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': PROJECT_DIR + 'db/db.sqlite', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
 
 
MEDIA_ROOT = PROJECT_DIR + 'media/'
MEDIA_URL = '/site_media/'
 
 
TEMPLATE_DIRS = (
PROJECT_DIR + 'templates/'
)

Finally, __init__.py is created in that directory to make it a python package, and the line "from development import *" is added to make it grab everything from the development.py file.

Source Control

I've been using Git lately, but as long as you're using something you're doing it right. Create a repository and use it.