Practical Django Projects: Writer better web applications faster, and learn how to build up your own reusable code library (buy)
James Bennett, Django Release Manager
Apress 256 pages
Any web developer worth their salt has undoubtedly spent significant amounts of time and billable hours building (and re-building) a library of reusable code to ease future development investment on their part. If you’ve made it that far, then you’ve probably also come to a point when you’ve scrapped all the code you wrote and picked from one of the many open-source web frameworks that have done all those tasks (and more), done them well, and that–most importantly–other people maintain.
When many individuals took their first steps in web development, they were forced to write much of their code from scratch. That includes handling cookies and sessions, talking with the database and building your own SQL queries, engineering some sort of templating system (or none at all in much of PHP development), and many more man-hours of boilerplate.
These days, from ColdFusion, Ruby, PHP, Perl, to Python, developers at all levels are turning to web frameworks to do much of the dirty work.
Django is a Python-powered web framework and it's one that I’ve been a following closely since its public inception. Django has had some big wins lately with Goolge’s App Engine and is well on its way to a 1.0 release. Django has been heralded as a well-managed and disciplined open-source project that has managed to garner praise for it’s stability and performance.
Practical Django Projects is an excellent book that goes well beyond The Definitive Guide to Django, which marches through each major bit of Django with basic examples of these features. The main downfall of the Definitive Guide was that while it is extremely illustrative of the different parts of the framework, it has little to no example of how to build a real-world project you might present to a client.
This book has a strong focus on building real, battle-tested web applications from the ground up and is split into four logical portions which are prefaced with a brief introduction to Python, Django, and web frameworks themselves.
The first major section is a basic content management application which introduces the concepts of using third party applications, in this case django.contrib.flatpages, to build a project. The author demonstrates how you add these third party applications, wire them to URLs, set up your templates, and get going. James' extends the CMS application in subsequent chapter by demonstrating how a developer might add interesting flourishes such as Markdown formatting, and a search system (writing your first custom view).
There ancillary chapters are used to introduce more complex ideas and more in-depth parts of the Django framework to readers. In this section for example, adding the search system tackles building a data model for the first time.
Building real projects
The first is a personal weblog replete with multiple authors, comments, tagging, categories, link blogging (with del.icio.us integration). From here, the book dives into more advanced subjects such as using Django's advanced template inheritance in this real world example. Readers are encouraged to go a step further by constructing custom templating tags, a task that eludes some Django developers until well into their career.
In addition to building the base weblog, advanced walk throughs on topics such as comment moderation (via Akismet), utilizing Django's email framework to send notifications when new comments are posted, and using the built-in syndication framework to create a series of RSS feeds for the site.
The second major project is one that powers a popular website in the Django community, DjangoSnippets. DjangoSnippets is an online application that allows users to upload short code snippets with descriptions. Users can sign-up for accounts, rate snippets, bookmark snippets.
Again, the author walks you through the entire process (spanning many chapters) of using Django’s built-in Generic Views to avoid writing code for common web application scenarios (list/detail views, etc.), user registration systems, adding in syntax highlighting, and advanced form handling and processing.
The final, and best, section of this book covers a few topics that have personally revolutionized my understanding and how I think about developing my Django applications. James goes to great lengths to explain the philosophy behind Django’s concept of reusable applications.
The idea here is that one should strive to develop small, tightly-focused, and loosely coupled applications (or modules) that can be plugged into any application to add instant functionality. In fact, a large portion of what makes Django such an attractive framework—it's auto-generated admin interface, and copious add-on library—are developed in this exact same fashion. It's one of the reason's Django is so powerful and easy to adopt.
James’ own django-contact-form or Nathan Borror’s blog, places, people, profiles applications are excellent examples of this philosophy. This chapter details logical tests that a developer can apply to his applications to determine if any portions can or should be split into their own reusable applications. James then goes on to demonstrate a series of best practices a Django developer can adhere to when writing their URL handlers, views, and templating structure to ensure that their applications can be reasonably customized by another programmer. In the final pages of this section, James goes over the process of packaging your applications for easy distribution with distutils and handy documentation tips.
With few Django books on the market at this point, I would recommend Practical Django Projects to anyone who has at least cursory experience with web development or web frameworks. By going through the two substantial projects in this book, a competent developer can hone their Django skills to a level that some have reached only after months or years. This book is short for the amount of useful information it will impart on you; at a thin 256 pages, you’ll be speeding through chapters at a nice brisk pace that satisfied my scatterbrained personality.
If you’re seeking a reference text containing of the high points of the Django API, you can always stick to the online documentation (which is excellent), or pick up a copy of the Definitive Guide to Django.