December 30, 2007

Starting to Understand Django

Given a fairly quiet holiday season I wanted to try and make some sense of Django, having formerly given TurboGears a look (though not yet having written that up). Both are capable systems whose designers I have a lot of respect for, so it would be nice to master their elementss. Each in its own way has the potential to improve web productivity, though it remains to be seen whether I have the headspace for all of both systems given their complexity.

After a couple of days messing around with Django I am at the stage where I have imported the Holden Web database model using Django's introspection features. A deal of cleanup was required, as well as a certain amount of reordering—most of which would not have been necessary if I'd had the sense to delete the Django admin tables from mo model, since I doubt I will need to manipulate them directly. Anyway, it's rather nice to see that the following views.py can generate the almost home page shown above:

from django.http import HttpResponse
from django.template.loader import get_template
from mainapp.models import Section

def homepage(request):
seclist = Section.objects.all()
hpdict = dict(("secPathZone%d" % s.sechomeslot, s.secpath) for s in seclist)
return HttpResponse(get_template("homepage.html").render(hpdict))


This is neat, though I have an idea that rendering the unnumbered lists to add the missing link sets is going to involve me in a little more work than I have had to do so far. Still, it's an encouraging start. I am serving this web from a local Apache server using mod_python to control Django and serving static content (images, CSS files) directly.

5 comments:

Unknown said...

Why did you go through manual HttpResponse, get_template and render instead of using the render_to_response helper?

Steve said...

I just posted a snapshot of the code in development. I suppose technically I should also have wrapped the hpdict as a Context, but ignorance was bliss ;-)

Anonymous said...

Steve,

I almost never wrap the dictionary in a context object. Using django.shortcuts.render_to_response, you can just do:

render_to_response("homepage.html", hpdict)

As you get into django more, you'll start to realize that there is more than one way to skin your cat, and then there are also ways to skin parrots, dogs, orangatans and fruitbats. It's a large animal, and you won't get in all in the first swoop.

Welcome to Django!

Steve said...

Thanks, Paul. I am still wondering what the advantage of a Context is over a dictionary for rendering templates. I guess the real advantages become more obvious when you start to add things like defaulting behaviors in subclasses such as Django's Request.Context.

I have been pleasantly surprised so far at how simple Django makes it to render the site—I have already got the home page completed, and am looking at a couple of performance optimizations in the client-side scripting before approaching generation of the body pages.

Obviously there will be mistakes on the way up the learning curve, and the jury is still out on whether it will be advantageous to move away from an almost entirely static site like holdenweb.com is today.

Steve said...

Further note: though I am not yet much further into Contexts one aspect I have only just become aware of is their stacking abilities. This allows them to act like the nested namespaces we are used to dealing with in Python, and while I haven't yet made use of the feature I can see it will be useful in certain circumstances.