New: iPhone/iPad development course in Belgium in August.

21 Mar 2008

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:

Add Your Comment

Spam Protection by WP-SpamFree

Getting started with Django on OS X

  1. Glad to see that you got it going, I went through the same thing a few months ago. I just recently launched my first “real” Django project (http://utah.travel) using DjangoAMF. I have to say that using the Django back-end saved me a tremendous amount of time and the project came in 30% under budget, you’ve just got to get used to working with the command line, which is easier said than done.

    If you end up looking for Django hosting, I’ve had great luck with the Media Temple Beta Django container…

    Dane Hesseldahl
  2. Hi Aral,

    Thanks for putting this tutorial up. I looked last week whilst at SXSW after a conversation with Steve Marshal about Django for a bit on installing it on the Mac, now you have provided.

    One of the hosts I use, Railsplayground.com, has Django installed and ready to go as well as Rails.

    Hope all is well.

    smiles, jen ;o)

    Ms. Jen
  3. p.s. I like the new masthead for your site.

    Ms. Jen
  4. Hey Jen,

    You’re very welcome. Do let me know how you get on. (And thanks for your kind words on the masthead) :)

    Aral
  5. @Dane: Congratulations on your new site and thanks for the tips :)

    Aral
  6. [...] If you’re going to follow along, you’ll need Adobe Flex (2 or 3), a copy of SWFObject, and a working Django installation (here’s how I got started with Django on OS X). [...]

    Simple deep-linking in Flex (and Flash) using Django at Aral Balkan
  7. Hi Aral,
    very usefull stuff, thanks a lot. One thing though – I get stuck when trying to run the server. I get
    Traceback (most recent call last):
    File “manage.py”, line 2, in
    from django.core.management import execute_manager
    ImportError: No module named django.core.management

    Any suggestions?

    jbruy02
  8. would this method work on 10.5.x (Leopard)?

    jeff
  9. Thank you, this was just the little extra information I needed to follow the official Django tutorial.

    Marc Haussmann
  10. Glad it helped, Marc :)

    Aral
  11. I created a project I call ‘Instant Django’, which provides what you’re looking for on Windows:

    htpp://www.instantdjango.com

    There is also a short tutorial that introduces some of the fundamental Django concepts.

    Charlie Lesh