Table Of ContentDjango JavaScript Integration:
AJAX and jQuery
Develop AJAX applications using Django and jQuery
Jonathan Hayward
BIRMINGHAM - MUMBAI
Django JavaScript Integration: AJAX and jQuery
Copyright © 2010 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the author, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
First published: January 2011
Production Reference: 1291210
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-849510-34-9
www.packtpub.com
Cover Image by Vinayak Chittar (vinayak.chittar@gmail.com)
Credits
Author Editorial Team Leader
Jonathan Hayward Akshara Aware
Reviewers Project Team Leader
Jake Kronika Ashwin Shetty
Michael Szul
Project Coordinator
Acquisition Editor
Joel Goveya
Steven Wilding
Proofreader
Development Editor
Sandra Hopper
Maitreya Bhakal
Production Coordinator
Technical Editors
Aparna Bhagat
Vanjeet D'souza
Conrad Sardinha Cover Work
Aparna Bhagat
Indexers
Hemangini Bari
Monica Ajmera Mehta
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, "OOhh,, iitt's a general purpose programming language
very slightly adapted for templating,"" iiss aa rreecciippee ffoorr nneeeeddlleessss ffrruussttrraattiioonn aanndd
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
About the Author
Jonathan Hayward as a child ranked 7th in a nationwide math contest, and
later programmed a video game on his calculator. He holds master's degrees in
bridging mathematics and computer science (UIUC), and philosophy and theology
(Cambridge). Jonathan has lived in the U.S., Malaysia, France, and England, and
has studied well over a dozen dialects and languages. He wears the hats of author,
philosopher, theologian, artist, poet, wayfarer, philologist, inventor, and a skilled
web developer who holds a deep interest in the human side of computing. He has a
website showcasing his works at http://JonathansCorner.com and can be reached
via e-mail at jonathan.hayward@pobox.com.
I would like to thank my parents, John and Linda, who love learning
and taught me faith, my brothers, Matthew, Kirk and Joe, my parish,
St. Innocent of Moscow, for a wealth of support. I would also like
to thank the editorial team at Packt: Steven Wilding, who helped
me come up with the book idea in the first place, Ved Prakash Jha,
who helped see it to completion, and Joel Goyeva, who helped
me with innumerable logistics along the way. And, of course, the
reviewers Jake Kronika and Michael Szul, who offered an invaluable
sharpening. The Django list, django-users@googlegroups.com, is
worth its weight in gold. I would like to thank Daniel Roseman, Alex
Robbins, Dan Harris, Karen Tracey, Oleg Lokalma, Mark Linsey, Jeff
Green, Elijah Rutschman, Brian Neal, Euan Goddard, Sævar Öfjörð,
"Ringemup", Ben Atkin, Tom Evans, Sam Lai, and Preston Holmes.
Authors have to leave somebody out who deserves to be mentioned;
that's just part of the territory. But I would like to thank one person
in particular: the reader. You're really the reason the book is here,
and you've chosen to invest some money in a book and some time in
fascinating technologies and let me help you along the way. Thank
you so much.
About the Reviewers
Jake Kronika, a web designer and developer with over fifteen years of experience,
brings to this book a strong background in frontend development with JavaScript
and AJAX, as well as exposure to the Django framework.
Having earned a Bachelors of Science degree in Computer Science from Illinois
Wesleyan University in 2005, with a minor in Business Administration, Jake went
on to become Senior User Interface (UI) Specialist for Imaginary Landscape, LLC, a
small web development firm in Ravenswood, on the north side of Chicago. In this
role, the foundations of his strengths in Cascading Style Sheets (CSS) and JavaScript
(JS) were built, as well as extensive use of Python and the Django Framework.
From there, Jake went on to work for the Sun-Times News Group, owner of the
Chicago Sun-Times and numerous suburban newspapers in Chicagoland. It was in
this role that he was initially exposed and rapidly became an expert with the jQuery
framework for JS.
Following an intermediate position as Technology Consultant with Objective
Arts, Inc, Jake has worked as UI Prototyper for JP Morgan Chase since February
2010. Since 1999, he has also operated Gridline Design & Development, a sole
proprietorship for web design, development, and administration.
I would like to thank my wife, Veronica, for her ongoing support.
She and my twin children Mykaela and Kaden provide all the joy I
could want in this world.
Michael Szul has designed and developed software applications for Fortune
500 companies, including AIG and Praxair, since 1998. Later, he served as a senior
software engineer for the technology division of Performance Media Group,
contributing to their fast growth and success, including placement on the Inc. 5000.
Szul's expertise in social software development led to a lateral move within the
company to become the director of development for their travel social network. He
even built successful social software for companies such as Apple Vacations and
Conde Naste's Gourmet Magazine.
As a partner at Barbella Digital, Inc., he currently designs and develops enterprise-
level workflow systems and mobile applications for educational institutions.