My Book reviews /

Python Web Development with Django

Title Python Web Development with Django
Authors Jeff Forcier, Paul Bissex, Wesley Chun
Publisher Addison-Wesley Professional
Date 2008-11-03
Pages 408
ISBN 0132356139
Rating

This is a very balanced book. It is probably a bit unusual to describe a book as balanced but here is what I mean. These days, programming is more like solving jigsaw puzzle. There is no silver bullet anymore. Earlier, programming meant just knowing programming language. If you know C then you are systems programmer. If you know Fortran then you are scientific programmer. Today, you really need to know 3-4 programming languages to be able to program the Web.

In fact, Web has become a crossroads of a bunch of technologies. In particular, it unites two schools of thought: traditional programming with functions and objects (languages such as Javascript, PHP, Python, etc) and declarative programming, or UI programming, when the programs you write do not have any sequence of commands. Instead, those declarative programs specify the layout of objects on a web page (HTML) and their style (CSS).

Therefore, to achieve the goal of web development one has to know at least the following: HTML, CSS, Javascript, and either PHP or Python, that is 4-5 completely different languages. In a certain sense Web has already become the Babel tower. So how is it possible to write a relatively short book that will teach you web programming if you have no background in either of those langauges? I think this book achieves this goal rather nicely, and this is why I call it balanced.

The book starts by providing a 1-chapter introduction to Python. I never thougth that it is possible to teach Python in 1 chapter, but after reading this chapter I found it pretty concise and useful. Of course, it assumes some elementary knowledge of programming, but from there it takes you to some advanced concepts such as lamdba functions and decorators in just 10 minutes worth of reading.

Without further ado, the book describes how to build a real-world useful application, namely a Blog explaining concepts of Django framework along the way. Such an example-driven approach keeps reader engaged all the time. Every time I open this book I think: what kind of cool application am I going to build today? This book contains four large-scale projects which illustrate various components of Django. On the other hand, there are some chapters with more in-depth description of underlying technology.

Django framework is a good example of software design. As it has to bridge two different approaches to programming, namely declarative (HTML) and functional (Python) methods, a clear separation of these is necessary to avoid code mess. In addition, Django presumes that each Web application needs a data store, and therefore some langauge for manipulation data. Each database has underlying schema, therefore Django has to include an object relation management layer. In short, Django assumes the following workflow of a typical web application:

  • Web application stores data in a database (blog posts, pictures, etc.). So, the core of every application is a data model, written in Python

  • So weтАЩve got some data. How are we going to show it? Here the model-view-controller (MVC) paradigm comes to rescue. First off, a view is a function that takes incoming HTTP request and generates a response. It can access data store through the data model defined in step 1. So a view function is also implemented in Python.

  • Now comes the tricky part. The view function fetches the right data, but how would it present it? It needs to generate some HTML, and the latter is a declarative language. Generating HTML from Python is a tedious task. This is why Django comes up with its own template engine which is a declarative langauge itself. It naturally coalesces with HTML, yet augments it with minimalistic functional features, such as loops, to iterate through collections of data.

  • Last but not least, a web application typically has many views, such as archive of all posts, recent posts, etc. A special regex-based mechanism is used to distribute incoming requests to appropriate view functions.

Django comes with a bunch of helper applications (called contrib apps in Django). One of them which is particularly useful is admin app, which makes it possible to enter data into the data store, according to the applicationтАЩs data model. Essentially, it is a nicely written UI for accessing the database. There are some contrib apps for managing users and sessions, and even for localization.

Despite all the complexity of a web application, Django does not hide the low-level details from you. You have access to every single byte of the web pages generated by Django. It only affects the way you think, it teaches you to write your software using design patterns. Therefore, Django is a good thing for universities тАУ students should learn how to design software, instead of learning specific programming language such as Java.

I think that this book makes it very clear that with Django you can start building cool stuff after spending weekend to read this book. This is a great recreational reading for a computer geek, and I definitely enjoyed it.