Sunday, May 17, 2009

Review of Django 1.0 Template Development

Initially when Packt Publishing sent this ebook Django 1.0 Template Development by Scott Newman to review, I did what any self respecting Django entreprenuer with 3 project down his pants would do. I skimmed through the book to the code sections and read through the code and read about 10 lines after that. The blink that I obtained reading the book this way was not too good and I felt some of the explanation in certain sections lacking.

BAD mistake! This book SHOULD NOT and I repeat SHOULD NOT be read that way. After I hunkered down and read the book cover to cover, it was then I really began to like it. So now, Django projects later, I wish that I read the book the right way the first time, because it has given me so many ideas to improve my Django code. So my advice, get this book, read it cover to cover and then revisit, bookmark or underline sections that you will revisit later. This way I found out was the way to get the maximum benefit out of this great Django reference. I really like reading technical books that know how to put forth advanced concepts with sounding too technical. Good programmers do not automatically make good teachers and the ones that do are the rare breed. On that note I loved the simple way in this book was presented. So, my kudos to Scott for making this an easy understandable read.

This is a book for Django developers who already have the basics and have been playing around with the Django examples from the website. The writing is to the point and easy to understand and practical examples are littered around to help the Django developers get their heads around what is being discussed. Perfect book to have by your side to try out code as your read. While the examples are simple, you could easily incorporate the examples into a real project.

My guess is that this book was not really meant for the total novice to Django. This book is meant for the mid level Django developer who wants introduction so some deeper concepts in Django especially those who want to find out more about Django's templates. That being said however, some sections seem to be crying out for more explanation.

The section has a very useful section regarding splitting up of the file for bigger projects. I really like this! It shows a bit about the whys and hows about proper Django deployment that is not usually covered the normal docs and mostly has to be scraped of mailing lists. The section about views is covered briefly but does contain a few useful examples and explains the concepts of requests and responses well using a few of Django's built in functions. Now what would have made this part of the book exceptional would be if the author covered best practices of deploying enterprise Django applications such as splitting the into apps to avoid the getting too big or unwieldy. The generic views section gives a good and solid explanation regarding one of Django's more powerful functionality that is the generic views. Middling Django developers should find this section of the book a useful reference as well as guide in their projects.

The next chapter is all about Context and how to use them in Django. I like the examples here and they work well to explain the concepts about Context. What the author could have done to solidify the examples here would be probably to include an example of how a normal call to an object would be done versus the method of using the Context object. The examples in this section are simply superb! They assume minimal knowledge from the beginning and build up to cover some of the more complex topics in Django without ever letting the example getting too heady. One thing I learnt here is the alters_data attribute that is actually included in all the templates preventing a method being called from the templates. It serves as an introductory to this functionality but I would have liked it more if a bit more explanation was given as how this attribute is put together with the save and delete methods. A bit more explanation about what kind of data is carried in the request object would be nice here. Scott teaches us to write our own context processor with a simple example of returning the user's ip address.

In my opinion Django's tags and filters are most of the time underestimated in the light of some other more powerful templating engine for example ZPT in Zope, but understanding them will make you understand the power of them and how much you can really do. I rely on these guys most of the time in my projects and they are great to use. Here Scott goes through most of the tags and filters that you probably would use in your project life and more. If you ever wanted to understand what is the difference between tags and filters read the book! I understand now and he does it in a one liner! The next chapter is equally good focusing on loading and inheriting templates. Don't blast over this chapter thinking it's not important (I was tempted to). Scott cover one or two things you might not have known about your I learnt a thing or two here namely the fact that you can get away by even distributing your template files as eggs! Template inheritance in Django is another one my favourites and Scott does a good job covering the basics using easy to understand diagrams, much like how I would explain the concept of template and inheritance to a template newbie, read it through though as Scott delves a bit deeper into templates with a few advanced concepts. The concept of multiple templates are covered next and the simple examples covered here are so easy to understand I could not wait to just to my Django instance to knock out the examples and try the out! Extending the section on creating a section for mobile content, Scott shows us how to employ the use of session variables in writing a middleware to check for mobile user agents. Good stuff this, you could if you wanted to copied section of this code and make it parf of your site.

In the custom filter section, Scott offers a simple example and the nice thing here is he slowly builds in advanced concepts without getting too Django technical. The example is easy to follow and beginners will easily pick up filter writing skills. One issue I ran into when trying out the examples is that this section does not cover some of the strange error messages that the template filters shoots out if you do not follow the syntax exactly. On that note this book could do well with some examples showing the errors that might be encountered in the examples.

One of Django's “killer app” is the admin interface. With just a one liner you get a beautiful web interface to edit your objects with all the forms and validation thrown in. While this is all good, the templates for admin interface takes some digging in and sometime to understand. I used it for one of my projects and let's just say one or two times I had to think hard just to get a column to align. Doing some wishful thinking I wish now I have a Delorean so that I could take a copy of this ebook and pass it to my head scratching self in the past. That would have really sped things up and made life easier for me as Scott does a superb job here of explaining the intricacies of the admin templating system.

On the whole I would gladly add this book my collection of Django books as a good source of reference. While it does not cover all aspects of Django, the section that it does cover, it covers thoroughly and I find that it's very useful in satisfying my curiosity to find out more about Django's templating system, filters and tags. The information that it presents is very practical and good to be applied in projects.

Tuesday, May 12, 2009

Unexpected behavior of python's ConfigParser

While working on some python code today I ran into some scoping issues that involved ConfigParser. The issue that made the process of finding and fixing the issue in the code harder is an unexpected behavior (on my end at least).

The situation is like this. My script relies on a configuration file that resides in the same directory as my script. For example sake let's call this file example.conf. During the execution of my script however, the directory scope of my script is no longer at my script main directory, so when it tries to find example.conf again it fails. This however is the part that put me off. The part that put me off is the fact that the error coming from ConfigParser is not really descriptive.

Take this example for example:

">>> config_file = 'does_not_exist_stuff.conf'
>>> config_parser = SafeConfigParser()
[] <------------- shouldn't this part shot up some error messages?
>>> config_parser.get('config1','test_config')
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib64/python2.6/", line 531, in get
raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'config1'"

The thing I am trying to say here is when I try to feed SafeConfigParser a config file that does not exist should it complain? I understand that the onus should be on my code to check whether or not the config file exists but then an error saying "No section:" does not really help too much in finding the error fast. I am writing this post just hoping some one would chip in and show me if the way I am using ConfigParser is wrong or something if not I am considering writing a patch for ConfigParser.

On the other hand I found another Python configuration parser: Going to look at this later on.