Django New Project Checklist

2011-01-18 14:38:00 -0800

Every time I start a new Django project, I also have to go back through other projects to find all the tweaks I make to customize Django to my liking. Here are some basics, and I'm sure I'll be tweaking this as I write more and more Django apps.

If you don't already have Django installed (assuming you're using Debian/Ubuntu)

sudo apt-get install python-django

Start a Django project: startproject example
#On my Ubuntu 10.04 box, this works instead:
#django-admin startproject example

Project naming note: Django won't let you make names with dashes in them (only numbers, letters, and underscores). So choose wisely! I generally do all lower case and underscores.

Enter the project and make the first app:

cd example/
python startapp exampleapp

Create a useful .gitignore: (If using git that is)

nano .gitignore


Get the settings and database ready:

touch sqlite3.db

#Add this near the top
import os
dirname = os.path.dirname(globals()["__file__"])

STATIC_DOC_ROOT = os.path.join(dirname, 'static_media/')

#In DATABASES replace:
#'ENGINE': 'django.db.backends.',
#'NAME': '',
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(dirname, 'sqlite3.db'),

os.path.join(dirname, 'exampleapp/templates'),
#Add this also if you want easy registration for your apps
os.path.join(dirname, 'registration/templates'),

#Under INSTALLED_APPS, uncomment/add:
#If you want registration, also add this:

#Final django-registration settings:

EMAIL_HOST_PASSWORD = 'yourpassword'

I really like most conventions laid out in Zachary Voase's blog, so I go ahead and create the appropriate directories and files. (Note: I don't follow all of his conventions, as you will see).

From the top level of your project:

#Let's layout the directory structure first
mkdir static_media
mkdir static_media/css
mkdir static_media/img
mkdir static_media/js
mkdir utils
#This will make it so we can import from utils
touch utils/

#You should always have a readme!!!
touch README

#And I like to include a doc or two with the project (usually just copied down from the wiki)
mkdir docs

We should also go into to make the admin site available. We're also going to make it easy to manage all of our urls for each app in this file in a readable way.


#Uncomment all of these lines:

# from django.contrib import admin
# admin.autodiscover()
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# (r'^admin/', include(,

#Add this outside the parantheses of urlpatterns:
urlpatterns += patterns('exampleapp.views',
    #Add urls in here

And finally, we need to set up the admin database (just answer all the questions):

python syncdb

Lastly, if you need it, you should install django-registration into your app directory (Though at this point, django-registration is getting quite old, so keep that in mind. Anyone want to help and fork it?)

tar xf v0.7.tar.gz
rm v0.7.tar.gz

Also, you may want to put your project under git source control, sync it to GitHub, and add a dev and testing branch to the git project. (Steps mostly taken from GitHub's help pages)

If you haven't already set your username and email, do so now:

git config --global "Git Guru"
git config --global

Now we need to initialize this directory as a git project and add all our files to git. Again, this is from the top level of your project. (This is a handy command, worth of an alias, such as 'ga' for git all, on my machines)

git init
git add -A --ignore-errors

Now let's commit and get it up to the git server (by now you should have created your first git repo, and seen similar directions)

git commit -m 'Initial commit'
git remote add origin
git push origin master