Book Image

Django JavaScript Integration: AJAX and jQuery

By : Jonathan Hayward
Book Image

Django JavaScript Integration: AJAX and jQuery

By: Jonathan Hayward

Overview of this book

<p>You want to create an AJAX application. Why would you use Django? Why would you use jQuery? Why would you use both together? Enter <i>Django JavaScript Integration: AJAX and jQuery</i> &ndash; your comprehensive answer to all these questions and the only extensive, practical, and hands-on guide to developing any AJAX application with Django and jQuery.</p> <p>Gone are the days when you used to lament over the lack of official documentation on AJAX with Django. This book will teach you exactly why Django is called "<i>The web framework for perfectionists with deadlines</i>", how jQuery &ndash; the "<i>write less do more</i>" JavaScript library &ndash; is practically a virtual higher-level language, and why they both deserve to be integrated with AJAX.</p> <p>This hands-on-guide shows you how to put Django and jQuery together in the process of creating an AJAX application. In this book, they are brought together in a real-world scenario, with attention to usability, to build and develop an AJAX application.</p> <p>The first two chapters provide a short and necessary introduction to the world of Django, jQuery, and AJAX; and the remaining chapters are based on a case study that will make you realize the immense potential and benefits of integrating Django and jQuery with your AJAX application.</p> <p>By the time you are done with this book, you'll be developing your AJAX applications with Django and jQuery in less time than you can say "integrate".</p> <p>You will cover the basics of AJAX; use jQuery, the commonest JavaScript library, on the client side, and learn form validation with an eye to usability, build things with Django on the server-side, handle login and authentication via Django-based AJAX, and then dip into the rich jQuery plugin ecosystem to build in-place editing into your pages. You will add auto-complete functionality courtesy of jQuery UI, easily build forms with Django ModelForm, and then look at a client-side search implementation that can look things up without network access after initial download. You will learn to Implement a simple, expandable undo system, and offer more full-blooded account management, tinker, fix some bugs, offer a more usable way to handle password input, add local time support for people who are not in your time zone, look at usability, and finally take a look at debugging.</p> <p>After working through this book, you will have both an AJAX application and a deep understanding that you can use to customize, extend, and further develop it in your organization.</p> <p>This book has been written and tested for Django v 1.2.3 and jQuery v 1.4.4.</p>
Table of Contents (20 chapters)
Django JavaScript Integration: AJAX and jQuery
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Foreword

In this book, we will be exploring Django JavaScript integration and build an Ajax application using jQuery. We will build a Web 2.0 intranet employee directory, and we will aim for a solution that is Pythonic in more ways than one.

Web development that is more "Pythonic" than just Python

This book is intended to be a book about how to do web development in the spirit of Python++.

The term "Python++" as we use it here is not the usual "Python is great," even if that may also be our opinion, but a more direct analogy to C++. When the effort was made to improve the C language, the basic kind of improvement attempted was to expand and broaden the core language. It is no accident that Stroustrup's The C++ Programming Language is far longer than Kernigan and Ritchie's The C Programming Language. The latter is a small book describing a small core language, while the former is a large book made large by the large core language it describes. The analogy intended by Python++ is somewhat loose here, and specifically does not include a large, or even a small, expansion of the core language. It is possible to tinker with the core language—easy_extend lets you extend Python to include a do-while loop (where the test condition is first evaluated at the end, not the beginning)—or add primitive syntax so you can do things like if remote_ip in 10.0.0.0:, but this is almost beside the point.

The real possibilities for expanding Python do not need to radically expand the core language, or change the core language at all. Django (http://www.djangoproject.com/) is not a general purpose enhancement to Python: if you are automating system administration tasks, for instance, you very probably have no reason to use Django. But for a limited problem domain, namely certain kinds of web development, Django is more Pythonic than Python. Python's cgi module is good for some kinds of small and simple tasks, but if you are going to make a serious, large-scale web application with many standard functions, using Python core language + Python standard library + Django is a fundamentally more Pythonic approach than just Python core language + Python standard library alone.

On StackOverflow, someone asked the question, "Have you considered using Django and found good reasons not to?" There were various answers, but the answer with the most "up" votes by far said, "I am an honest guy, and the client wanted to charge by the hour. There was no way Django was going to make me enough money."

Django itself is not the limit to Python++. Pinax (http://pinaxproject.com/) is built on top of Django and offers a "more Django than Django" platform to build a social network. Satchmo (http://satchmoproject.com/) is also "more Django than Django" for another narrower focus: e-commerce webshops. And there are other platforms built on Django; it is time well spent to search the Python Package Index (http://pypi.python.org/pypi) for Django to see what is already available. In this text we will often use "Django" as a shorthand for either basic Django or any of the many good tools built on top of Django.

Depending on what you are trying to do, it may be that the bulk of the Python work in Django is resolved surprisingly quickly: you can build and brand a Pinax social network by doing little more than overriding the CSS and adding images. This book will address the Python side and try to give a solid basis for programming Python for Django, working with the templates, and so on, but that problem can often be solved so cleanly that most of the work that remains is styling and Ajax.

Django and its templating engine

Before further exploring technical details, it would be worth taking a look at the opinions and philosophy behind the Django templating language, because an understandable approach of, "Oh, it's a general purpose programming language very slightly adapted for templating," is a recipe for needless frustration and pain. The Django developers themselves acknowledge that their opinions in the templating language are one just opinion in an area where different people have different opinions, and you are welcome to disagree with them if you want. If you don't like the templating system that Django comes with, Django is designed to let you use another. But it is worth understanding what exactly the philosophy behind the templating language is; even if this is not the only philosophy one could use, it is carefully thought out.

The Django templating language is intended to foster the separation of presentation and logic. In its design decisions, both large and small, the Django's templating engine is optimized primarily for designers to use for designing, rather than programmers to use for programming, and its limitations are almost as carefully chosen as the features it provides. Unlike ASP, JSP, and PHP, it is not a programming language interspersed with HTML. It provides enough power for presentation, is intended not to provide enough power to do serious programming work where it doesn't belong (in the Django opinion), and is simple enough that some non-programmers can pick it up in a day. For a programmer, the difficulty of learning the templating basics is comparable to the difficulty of simple HTML or SQL: it is simple, and a good bit easier to learn than wrapping your arms around a regular programming language. It is likely that there are a number of Django programmers out there who started by asking, "Why doesn't the templating language just let you mix Python and HTML?" and after playing with it, found themselves saying, "This isn't what I would have come up with myself, but I really, really like it."

Additional benefits include it being fast (most of the work is done by a single regular expression call, and the founders talk about disabling caching because it couldn't keep up with the template rendering engine's speed), secure (it is designed so that it can be used by untrusted designers without allowing a malicious designer to execute arbitrary code), and versatile enough to generate whatever text format you want: plain text, HTML, XML, XHTML, JSON, JavaScript, CSV, ReStructuredText, and so on. We will be using it to generate web pages and JSON, but Django's templating language is a general-purpose text templating solution.

What we will do in this book—building a Web 2.0 intranet employee photo directory

Many books will teach you a new technology by walking through a sample project. This book is no different, but the sample project is not a toy: it is a walk through making a real, live Web 2.0 intranet employee photo directory that you can customize to your organization's needs. This is something that is both useful, and will give us a tour of the potential for developing Ajax applications using Django on the server side and jQuery on the client side.

- Jonathan Hayward