Here's a few notes I made while working my way through Django's tutuorial:
Installation
Installation of Django is easy. Just following the instructions on this page:
It can either be downloaded as a tarball or via SVN. We've chosen the tarball (0.92.2) option.
Nitpicking, the tarball URL doesn't end with the name of the tarball - it's a directory. This is only slightly annoying.
Our environment has as per-user Python installation, so we don't need to use "sudo" to install the Django packages
Testing (Part 1)
We are following the instructions son this page:
Running a development webserver
Django has options for running under mod_python, WSGI or using a standalone built in webserver. The built-in webserver is documented as not-for-production, but it's good enough to get going so we're going to play with that for now. Eventually we expect to use both the mod_python (because we have it here) and WSGI options (because it's both the way to go and the most efficient).
We immediately ran into issues running the webserver because it's not on the "localhost" - the browser said it couldn't find the server. In our environment we ssh to a linux server and access that from our desktop computer. After a little googling, the trick turns out to be adding a "IP:port" argument to the Python command:
python manage.py runserver 192.168.1.10:8000
Connecting to the DB
Next in the instructions is connecting to the DB. Again, we have issues here - no fault of Django - because of the uniqueness of the local environment. Like Python we run MySQL on a per-user basis so we need to be able to specify a fairly unique setup with a TCP/IP and UNIX domain socket. It's not clear initially how to specify the path for the UNIX domain socket but fortunately the stack traceback and clearly written code come to our rescue here: you use the DATABASE_HOST and the code looks to see if it's a path.
Creating the DBs
The ‘syncdb' asked if I'd like to create a super user for the DB. I said no and everything seems to be working - a bunch of tables show up in MySQL as promised.
Creating an App
An "app" is something that does something or something like that. The command works as promised.
Create the Model and DB tables
Works as promised. There appears to be some underlying cleverness happening as (for example) the "sqlclear" command knows whether the tables are their or not.
The "sqlinitialdata" refers you to another command; this is probably a mismatch between the code and the tutorial.
Interactive Shell
This is cute - running "python manage.py shell" will drop you in the environment that the webserver sees so you can do stuff on the command line to see how it will work.
Stylistically, I differ from Django in that I (almost) never do anything except ‘import x', as I prefer to use the dotpath and not have to guess when I'm skimming code as to where functions are coming from.
As an aside, it would be cool if Python examples did the ‘>>>' as an image or as an CSS trick so they don't get copied when cut-and-pasting examples.
Testing (Part 2)
And we're on to page 2 of the tutorial:
Admin Interface
And we get our first "uh oh" - we didn't create a "superuser account" on way back above, well, just because. Fortunately, Lord Google knows all and we quickly find out that you can run Python commands to do this and we're in business:
$ python manage.py shell
>>> from django.contrib.auth.create_superuser import createsuperuser
>>> createsuperuser()
Thanks to here for the tip (which also looks like interesting reading):
Admining the App
Nice - add an inner class declaration to App "Poll" and it now shows up in the Admin interface and we can do stuff with it. Neato.
I've also just discovered that I don't need to stop and restart the app - it's using reload().
Adding __str__ to the model makes the Admin app useful; if you do something wrong the HTML function is really quite clear.
You can change the way the Admin interface displays Poll by adding a ‘fields' declaration. This is a little bizarre - it's a tuple, of tuples with a dictionary inside. Why not a list of dictionaries?
Bug: if you use the ‘classes/collapse' option of the admin interface and there's a bug while saving an item while the item is collapsed, there's no indication of where the error is.
Changing Templates
The instructions are a little confusing - pay attention - but it works as advertised. There's a command called ‘adminindex' which dumps out template code but I'm somewhat confused by it and I wish there was a little more detail here.
Testing (Part 3)
And we're on to page 2 of the tutorial.
Design your URLs
Django converts URLs coming from user requests into actions by:
- looking at ROOT_URLCONF in settings.py
- loading mysite/urls.py (as defined in the settings)
- sequentially looking at regular expressions
-
loading a module - typically a view - and calling a function defined by the matching regular expression. This is
expressed as a dotpath - e.g. ‘x.y.z' will load module ‘x.y' and call function ‘z'
- the function is called with a request object and all the named groups from the regular expression - clever
-
also note that:
- you cannot filter on the hostname in the URL (problem?)
- regular expressions are compiled (and thus are very fast)
The code samples work as advertised. In a later section we'll learn that you can move all the URLs into the app so that you don't have to (a) put all the URL information at the project level (b) decouple the base path of the URL being used.
Write views that do something
This section explains how to returning meaningful results building on the knowledge in section 2.
- the template loading system rocks
- the shortcuts are very handy (i.e. lots of common actions are compressed into single function calls)
Playing with forms (Part 4)
This section is a mess. I'll probably retackle it.

