thraxil.org:

Review: Django 1.0 Template Development

by anders pearson Wed 13 May 2009 16:50:43

I don't usually do book reviews here, but Packt Publishing was nice enough to send me a review copy of Scott Newman's Django 1.0 Template Development a while back and I haven't otherwise had much in the way of ideas for posts lately, so here goes.

Over the years, I've used probably a dozen different templating languages in a couple different languages, rolled my own, and contributed bugfixes to a few others. I remember years back when it seemed like everyone was writing Perl CGI programs that generated HTML with a million print statements and people looked at you funny if you suggested that a templating language might simplify things for them. Thankfully, as of 2009, the web development community seems to be on board with the basic idea of separating code from markup and all the advantages that that approach entails.

Of all those templating languages that I've played with over the years, Django's is currently my favorite. That's not to say it doesn't have its flaws. I don't think there's such thing as a perfect templating language; they have to balance too many things and will never be everything to everyone. Django seems to have come closer to the sweet spot than anything else I've used though. It's powerful enough that you rarely need to work around it but simple enough that once you learn the basics, you rarely have to look anything up. The syntax is remarkably clear and compact, and somehow, it's still really, really fast.

Any book on Django templates faces an uphill battle to prove its worth. Django's template language is a very simple syntax and has some of the best online documentation out there. I was certainly skeptical at first that there was really any room for a book on such a narrow and already covered topic.

Unsurprisingly, then, the book's section on the basics of the syntax is pretty short and to the point. More space is then spent just providing a reference section for the built in tags and filters. I certainly can see the usefulness of that. Sometimes the searchable online docs are what you need, and sometimes it's handy to have paper. The book's explanations of these built-ins aren't substantially different from the online docs, but do tend to go into a little more detail on specifics.

The meat of the book is really in the sections covering everything in Django that's around the templates and interacts with them. This turns out to be practically everything in Django (except maybe the ORM). The overview of how URLs are mapped to views, which load data into contexts that can be modified by Context Processors, process the contexts with templates, and return responses which might be further modified by middleware is probably not going to be very exciting to a programmer with much Django experience but I could see it being useful for someone new to Django or to, eg, a designer who's only touching the templates but ought to understand how they fit into the whole system.

Template loading and inheritance are covered in pretty good depth. Those are topics that tend to get glossed over but are actually of key importance to understanding how to structure large Django projects to allow for a great deal of flexibility and power. I find Django's approach to template inheritance very intuitive (if not quite as powerful as ZPT's) so I didn't need too much help getting up to speed with it, but I could see how it might be unfamiliar territory if you've only worked with 'include' style template systems and the book's explanation of how to think about structuring your templates is probably just what's needed. The template loaders that are included with Django are covered in detail and it does a good job explaining why they each exist.

A great opportunity was missed though as there's no discussion of creating your own template loaders. I've implemented a template loader that would pull templates out of the database before and I can say from experience that documentation on writing your own template loaders is sorely lacking. It ultimately wasn't very hard to do, but I remember having to actually dive down into Django's source code to figure out how to do it (I recommend reading Django's source code though; it's remarkably clear compared to other frameworks I've had to do similar digging in).

The chapter on writing your own custom template tags and filters is probably the most important chapter in the book. Django's template syntax is purposefully simple so one of the most common complaints I see is that it ends up pushing a lot of "display" logic into the views. This is almost always easily overcome in a nicely reusable and modular fashion with a custom tag or filter. The problem is that Django's documentation on how to do that is hard to find and very intimidating. It's there and adequate as a reference, but it's very easy for someone to get pretty far with Django without knowing that custom tags and filters are an option at all, or just thinking that they're too complicated to bother with. The reality is that they're pretty straightforward to implement once you've seen a good explanation of how they work, which this book finally provides.

There are also chapters on pagination, customizing the admin look and feel, and internationalization. Pagination, I think is covered pretty well in the online Django docs and isn't very complicated so I feel like that chapter is mostly filler. Customizing the admin interface is pretty straightforward and the chapter is superfluous if you've really understood the earlier chapters on template loading and inheritance since it's just a direct application of that. But I can see it being a useful example since the admin app is so widely used and customizing it is something of a Django rite of passage. Maybe if the earlier chapters didn't sink in, that would help pull it together. Internationalization, admittedly, I've just barely ever had to deal with so I tend to skip over that.

I'd be happier if the book got more into django.forms or really even discussed them at all. Designers who are working with django templates are bound to run across forms and have to deal with them. Eg, there's discussion of customizing the django admin interface, but in reality that almost always also includes tweaking the forms and not just the templates and CSS.

I think what I'm getting at is that while the book works well for a programmer looking to get deeper into everything in and around Django templates, I think it ends up being much less useful for someone more on the design end of the spectrum than I'd hoped. In other words, if I'm working on a Django project with a designer who's doing the HTML and CSS and doesn't know much programming beyond that, I had been hoping that I could hand them the Django Template Development book and it would serve their needs. Maybe that isn't what the author intended, but it's what I'd hoped for when I saw the title. But without a discussion of forms or aspects of the model that someone working on a template would probably encounter (eg, if they saw "{% if author.page_set.count %}" in a template, they ought to know something about how that relates to foreign keys, the _set convention, and querysets, at least to recognize the common pattern in use), I'm not sure it's going to provide them with anything more than a printout of Django's online template syntax and built-ins docs would.

TAGS: book review django templates

formatting is with Markdown syntax. Comments are not displayed until they are approved by a moderator. Moderators will not approve unless the comment contributes value to the discussion.

namerequired
emailrequired
url
remember info?