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.
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.
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.
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