Review: Django JavaScript Integration: AJAX and jQuery

Posted in Django on April 16th, 2011 by Stephen DeGrace Link
Topics: Reviews, jQuery, AJAX

A while back, I was asked to review the book Django JavaScript Integration: AJAX and jQuery, written by Jonathan Hayward, published by www.PacktPub.com. The company is looking for people who have blogged about the topic of the book to write reviews of it on their blogs - as this is a topic I have written about and I was very interested reading a book-length discussion on the topic for free, of course I agreed. If you would like a sampling from the book, a chapter is available for free online (Chapter No.10 - Tinkering Around: Bugfixes, Friendlier Password Input, and a Directory That Tells Local Time). I think this chapter is fairly characteristic of the book as a whole and will give you a good sense of what Hayward has to offer. I think some people would enjoy this book and get something out of it, but my personal reaction was more of disappointment. Overall, I'm glad I didn't pay good money for this book.

First of all, I have to get something out of the way: this is not a book I would ever recommend to beginners. Not because the treatment is at a higher level, but somewhat the opposite. To begin with, in the current edition of the book, the text introducing jQuery is riddled with basic errors which will confound beginners. I honestly believe that Hayward knows better, but this section of the book seems like it may have been rushed into print with inadequate proofreading and copy editing. Anyway, it's sloppy, and only readers who are reasonably experienced with jQuery will spot the errors without being lead down any garden paths. While Hayward is apparently much more comfortable in Python and the Django and these parts of the text are much more solid, I don't find that Hayward especially shines in this area from a technical standpoint, either. In general, the jQuery and Django sites still provide far superior documentation and basic tutorials for their libraries. Beginners would be far better served to review the tutorials provided on these sites - Hayward adds nothing in particular until you have acquired a strong grounding elsewhere.

What I was hoping for was a technical discussion on how jQuery and Django interoperate, and in this area I also found Hayward fell short for me. Yes, the basics were explained, somewhat. But Hayward doesn't honestly seem to like or trust Javascript or jQuery especially... to me, he almost seems to treat jQuery as the best of a bad lot of Javascript libraries. Maybe it's just me, but I happen to love jQuery, and I felt like it deserved better. It's discouraging for a technical writer to evidently have a tin ear for beautiful code. He doesn't focus on what I would consider mainstream jQuery practices, instead preferring to recommend eccentric or heterodox practices like using the $.ajax() method as much as possible to make AJAX requests, and avoiding using jQuery's own facilities for using JSON, apparently believing them to be a security risk (a point which I don't think is adequately substantiated). One fact I find positively risible is that Hayward binds events to elements in the old style via properties on those elements and completely bypasses the jQuery way of handling events, or for that matter any semblance of non-jQuery Javascript current best practices in this area.

I find there are eccentric elements on the Python side, too. This is another reason why Hayward's book is really only for experienced programmers - I'm not saying that Hayward's opinions are  necessarily wrong about these matters, although in some places I don't happen to agree, but they require engagement at the level of someone already familiar with the pertinent issues and would be unwise to take as Gospel. Like for example, he splits a user query into "word" chunks using the bit of code:

split_query = re.split(ur'(?u)\W', query)
while u'' in split_query:
    split_query.remove(u'')

Ignoring the ubiquitous u's, which is probably laudable but something I would not bother with,  why would you do this? Why not split on \W+ and not have to remove any empty strings in the first place (best option), or else use a list comprehension like split_query = [x for x in split_query if x != u'']? Again, Hayward's code seems rushed and inelegant, even in his area of greater strength.

What's missing is important workaday tasks in Django and jQuery integration like serializing forms in jQuery and feeding them to Django, and what to do with that data in Django. One critical issue that is missing is the $.ajax() method's traditional parameter, which if not manually set to true will cause surprising loss of backward compatibility for Django code written to operate with jQuery 1.3 and below (and requires extra processing steps for Django code written for jQuery 1.4 and up). This is related to jQuery optimizing for the behavior of PHP and Ruby on Rails, and is something Django developers need to watch for.

One thing that stood out for me: In his jQuery code, Hayward hard-codes  URLs, including some rather ugly ones with query strings built in, directly into his AJAX calls, saying (I paraphrase) that there is not really a good way to follow accepted Django best practices here of not hard-coding URLs in templates. If the jQuery code is generated by templates to which you have pushed the relevant parameters into the context, you simply have to add the URL you want to your urlconf in Django and use the {% url %} tag in your Django template. If you want to feed data to the view via the request rather than via the URL parsing, that is what the data parameter of the AJAX call is for - there is no need to manually hack together query strings. This is not a hard solution to implement, and emblematic of the kind of discussion I wanted to see which I feel is missing from this book.

In general, this is not a technical manual, and technical issues are not the author's primary focus and concern. If you come to this book hoping for an advanced technical discussion of how jQuery and Django, taken in their most conventional and mainstream style are best able to interoperate, you will be disappointed.

While Hayward's coding, at least as shown in the examples in the book, is mediocre at best and certainly cannot be said to sparkle anywhere, his philosophical system of user interface design which he uses Django and jQuery to illustrate is more adept, and it is on this level that the book should best be engaged. On this level it's a stronger book, although frankly in many places I found the philosophical excursions a little tedious and trite and not particularly enlightening.

Hayward makes a decent argument for his views on proper programming to interact with users. In a few places, I found Hayward's contentions about optimizing the user experience to be genuinely interesting and enlightening. Given the profusion of dreadful user interfaces out there, many programmers could certainly use to take such arguments under serious consideration. I can't help but feel, though, there must be better books on the market to address this kind of topic in a straight-forward way without dashing anyone's hopes of achieving a more technical enlightenment. There are also some practical ideas in Hayward's central example throughout the book, the photo directory, from which a reader may glean some useful ideas.

Comments:

There are 0 comments on this item. Be the first to comment.

Post a Comment

* :
* :
:
:

* Required field, your email will not be posted.