All Unkept
Posted in: Python, Web development, Django  —  March 03, 2009 at 08:52 PM

Review: Django 1.0 Template Development

by Luke Plant

A few weeks ago I received a free copy of Django 1.0 Template Development by Packt Publishing to review. You can view the chapter about serving multiple output formats on my blog, and below is my review of the rest.

Author

The author is Scott Newman, who has had several years' experience with Django, on top of more than a decade with other web development technologies, and his experience shows. He demonstrates very good understanding of the philosophy of Django's template system, while recognising its limitations.

Audience and approach

The book is clearly aimed at the same people that Django's template system itself is aimed at: web designers with experience in HTML and web development in general, but who might have very little experience with the programming side. The book assumes almost no knowledge of Python, and while it suggests that you go through Django's initial tutorial first, you could easily follow the book without doing so, and with essentially no Python knowledge. Each topic is presented using examples, and all of the technical bits required to get you to the point of actually working with the templates themselves are covered in flawless detail with nothing assumed i.e. creation of projects, apps, view functions (or using generic views), hooking things up using URL confs, running the development server, calling syncdb etc. After the first few chapters, most of the rest are essentially stand-alone, so you can skip chapters safely, and if necessary, you will be pointed back to any necessary setup steps for the examples. The book in its entirety also assumes no knowledge of things like regex syntax or even the HTTP protocol.

This approach is just perfect for web designers who need to get into Django templates, but don't want to learn Python first. It is also ideal for the likes of Jeff Croft, the designers-cum-developers, who want an easy way in to developing entire Django applications. One of the great things about PHP is that the progression from knowing HTML to writing dynamic sites is very smooth—you rename your .html files to .php and start adding dynamic bits. Of course, with PHP things tend to get ugly from there on, so a book that allows designers to take a similar approach with Django is very welcome.

As I've thought about this, given the vastly superior out-of-the-box approach that Django has with regards to security issues (such as SQL injection, auto-escaping and XSS), compared to PHP, I've realised that, armed with a book like this, Django could become the perfect choice for "My First Dynamic Web Site" projects. Securing a PHP web app is a job that is really for experts only, and despite years of experience in PHP, I increasingly feel myself not up to the task, and I positively tremble for all the novices out there stumbling through vulnerability after vulnerability in PHP. But with Django things are very different—the obvious way to do most things is secure by default. This idea—"Django is for newbies"—is a bit of a surprise to me, as I've always thought of Django as a framework for mature web developers, by mature web developers. If I'm right about this, Django really reflects Python itself beautifully—Python has this amazing ability to be just right for complete newbies, and yet provide a depth that rarely disappoints far more experienced programmers. The only problem left for Django is ease of deployment—PHP still reigns almost unchallenged in this area.

Scope and contents

So the scope of this book ends up being slightly broader than the title would at first suggest, but it actually makes perfect sense given this audience. The chapters are as follows:

  1. An introduction to the Django Template System
  2. Views, URLs, and Generic Views
  3. Template Context
  4. Using the Built-In Tags and Filters
  5. Loading and Inheriting Templates
  6. Serving Multiple Templates
  7. Custom Tags and Filters
  8. Pagination
  9. Customizing the Admin Look and Feel
  10. Caching Your Pages
  11. Internationalization

The table of contents is very detailed (and is available online), and there is also a comprehensive index.

The first chapter covers all of the architecture of Django and the request life cycle that a template author, and someone following the book, will need to use.

Chapter 2 goes into detail about how to construct views and supply templates with the context variables they need. It includes how to make simple models, which are used in the sample application. It then discusses how to simplify things by using generic views. Chapter 3 provides more details about Context, and goes on to cover using RequestContext and context processors, which is important as this will be very common, as well as using the shortcuts such as render_to_response, which is the right order to do things I think.

Chapter 4 provides a complete reference to the built-in template tags and filters, which exceeds that available online by providing examples for every instance. Chapter 5 completes the basics of using templates by going into how loading templates and inheritance works, and helpfully discusses the use of includes over inheritance.

The rest of the book really addresses various tasks that template authors are going to face, and does so very well in my opinion, consistently targeting the same audience and providing all the needed technical details. Chapter 6 distinguishes itself by a very helpful treatment of various approaches to serving different content to different browsers, particularly thinking about mobile devices, that I've not seen elsewhere.

Throughout the book there is generally very impressive attention to detail. It also follows Django best-practices and coding style, along with PEP 8, which I appreciate. I found just a handful of minor errors (which should shortly appear on the errata page).

There was just one significant error, which was in chapter 7, p164, which says that the is_safe attribute of template filters will be explained at some point, but it never is (that I could find, anyway). On this point, namely the issue of auto-escaping and how it works, I found the explanation of the escape filter to be quite helpful—the book essentially says that the escape filter is always applied last wherever it comes in a chain of filters, which is a nice way of thinking about this somewhat tricky feature. However, I did think that some more explanation, or at least mention, of the issues surrounding auto-escaping and implementing template filters, including the unexplained is_safe attribute, was in fact necessary.

Given the audience, I thought that the book could have been improved by providing examples of how to better exploit the powerful generic views by 'wrapping' them with a view function that does some initial work and delegates the rest to one of the generic views. This is a common pattern, but one missed by lots of newbies (who instead file tickets for how the generic views can be expanded) and there were a number of opportunities in the book which could have been used to show this useful technique.

These minor criticisms apart, I found the book very impressive and I'm happy to say that I would heartily recommend it for all template authors, and especially the type of developers and designers that I've mentioned above.

Comments §

blog comments powered by Disqus