Getting started with Django on OS X

I've started setting aside a few hours every night to try new things. Two nights ago, I felted. Last night, I played with my Arduino board and light sensors. Tonight, I wanted to play with Django.

I use the term "play" loosely.

The problem is that there's no easy way to get started with Django. You have to jump through a few hoops to prove that you're truly worthy. I can't help but compare that to how easy it is to play with PHP (install MAMP, WAMP, or XAMMP and Bob's your uncle; you're up and running with Apache, PHP and MySQL in a single-click.) Similarly, Rails had Locomotive in Tiger and apparently comes pre-installed in Leopard (that's not enough to make me go back to Tiger after downgrading.)

Django looks lovely, but for it to really go mainstream it has to be easy for developers to set it up and play with it. To kick the tires, as it were, without jumping through too many hoops. Django needs its Locomotive.

All that said, I was resolved to play with it so I decided to try and find the simplest way to get a Django development environment running under OS X. During my rants on Twitter last night, Gareth Rushgrove had kindly suggested XCode + Macports but I didn't want to go through the hassle of installing the monolithic XCode.

Here's what I did do to get Django running on my OS X box running Tiger:

  1. Simon Willison had mentioned on Twitter that SQLite is a good development database (and MySQL is notoriously difficult to set up with Python). Seeing that Python 2.5 came with everything you need to work with SQLite3, I started by installing the Python 2.5.2 Universal Binary. (Using Django's Object-Relational Mapping engine, you can apparently just switch it out for MySQL, etc., on deployment.)
  2. Next, I checked out the Django trunk from SVN using Terminal: svn co http://code.djangoproject.com/svn/django/trunk/ and followed the official installation instructions.

If you've been following along, at this point, you should actually have a working Django install that you can develop on using SQLite3. (That wasn't too difficult at all actually; but neither have I seen it spelled out like this anywhere else.)

To test your installation, bring up Terminal and type:

django-admin.py startproject mysite

You should see Django create the mysite folder for you.

Navigate to that folder (cd mysite) and look at the files that Django created for you (ls). You'll be using these to build your Django application.

singularity:~/django/myfirst/mysite aral$ ls
__init__.py     manage.py       settings.pyc    urls.pyc
__init__.pyc    settings.py     urls.py

Next, run the development server:

python manage.py runserver

You should see something along the lines of:

Django version 0.97-pre-SVN-7254, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Finally, to make sure that SQLite is working, edit the settings.py file in your mysite folder to populate the following two lines

DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = '/Users/aral/projects/mysite/mydb'

Replace the path with the path to your project folder. The mydb file doesn't exist yet. Create it by bringing up Terminal and entering:

python manage.py syncdb

You should see a stream of output starting with:

Creating table auth_message
Creating table auth_group
Creating table auth_user
Creating table auth_permission
Creating table django_content_type
Creating table django_session
Creating table django_site

Create a superuser account when prompted.

Finally, to see the schema of the database that Django has created for you, fire up sqlite3:

singularity:~/django/mysite aral$ sqlite3 mydb
SQLite version 3.1.3
Enter ".help" for instructions
sqlite> .schema
CREATE TABLE "auth_group" (
    "id" integer NOT NULL PRIMARY KEY,
    "name" varchar(80) NOT NULL UNIQUE
);
// etc.

I hope this helps you get started with Django on OS X. I'm looking forward to playing with it in the coming days.

Resources:

Comments