By Jake Edge
August 27, 2008
The Django web application
framework is nearing an important milestone: version 1.0. Like Ruby on
Rails, TurboGears, and others, Django is meant to streamline
application development for the web by providing easy-to-use libraries for
tasks that
are commonly performed by dynamic web sites, such as database access and
HTML templating. The Django project has just released
the second beta of the 1.0 release, with the final release due in early
September.
Django is Python-based, with an eye towards getting an application—or
the beginnings of one—up and running quickly. The framework is quite
"Pythonic", so it will be very accessible to those used to programming in
that language. Django also has an extensive set of
documentation including an on-line (and dead tree) book.
While Django can be used to build nearly any kind of web site, it has a
"sweet spot" that is well described in the introduction to The
Django Book:
Because Django was born in a news environment, it offers several features
(particularly its admin interface, covered in Chapter 6) that are
particularly well suited for "content" sites – sites like eBay,
craigslist.org, and washingtonpost.com that offer dynamic, database-driven
information. (Don't let that turn you off, though – although Django is
particularly good for developing those sorts of sites, that doesn't
preclude it from being an effective tool for building any sort of dynamic
Web site. There's a difference between being particularly effective at
something and being ineffective at other things.)
The database abstraction (or model) layer is at the heart of what Django
provides to a programmer. Most dynamic web sites use some kind of
database, so Django supports multiple, popular database systems, of both free
software and commercial varieties. Because the model layer is a high-level
description of the data, moving from one database backend to another is
greatly simplified. In addition, the flexibility of the model API means
that many applications can do all of the queries that they need without
ever descending into SQL—though the facility is there if it is needed.
An example taken from the book nicely illustrates the simplicity of
Django's model API:
class Book(models.Model):
name = models.CharField(maxlength=50)
pub_date = models.DateField()
From this information, along with some configuration concerning database
type and name, Django can generate and execute the appropriate SQL to build a
database table to store a Book. As fields get added and removed from the
model, the proper commands to synchronize the model and the database can be
generated. From application code (i.e. the "view" code), then, models can
be used in various ways, for instance:
def latest_books(request):
book_list = Book.objects.order_by('-pub_date')[:10]
return render_to_response('latest_books.html', {'book_list': book_list})
This can then be used in an HTML template as follows:
<h1>Books</h1>
<ul>
{% for book in book_list %}
<li>{{ book.name }}</li>
{% endfor %}
</ul>
That is, of course, a simple example, (and it lacks the URL mapping piece)
but it gives a flavor of the power
that Django provides. It is also an example that most folks, even
non-programmers, can follow to some extent. Like many
model-view-controller (MVC) based frameworks, Django splits up the various
pieces of functionality in an attempt to break the coupling between the
user interface, "business" logic, and data storage, allowing each to be
worked on separately. In particular, the template language is meant to be
used by web page designers who have little programming background.
One of the nicer features is the automatically generated administrative
interface. Many web frameworks have incorporated an easy way for a site
administrator to start entering data into their models. This allows
developers to get their application running quickly with real data
without having to code up a bunch of tedious data entry forms. One
of the bigger changes from the current 0.96 Django release and the upcoming
1.0 is a complete overhaul of this interface.
Many developers have been using the development versions of Django from the
subversion repository because the released version (which is what is
packaged by distributions) has lagged. There are a number of
backward-incompatible changes since 0.96 and the documentation is geared
towards the 1.0 version (though it should be noted that versions for each
of the last two releases are also readily available). Stabilizing the API
has been the driving force behind the 1.0 release. Going forward,
compatibility will be maintained unless a security or other
serious problem is found.
Django has numerous other interesting features: authentication, session
handling, and a caching system
that is geared towards scalability. It is also fully ready for
internationalization, with "full support for multi-language
applications, letting you specify translation strings and providing hooks
for language-specific functionality."
Due to be released on September 2, just in time for DjangoCon, 1.0 is, unsurprisingly, both
feature and "string" frozen—only serious bug fixes are still going
in. Like other projects, including Python itself, Django is "governed" by
an independent foundation;
the newly formed Django
Software Foundation in this case. The original developers of Django are still
active both as foundation board members and as active developers (and
users) of Django.
There are lots of web frameworks to choose from, in nearly every computer
language used today—though none for COBOL as far as we have
heard—so Django is just "yet another" web framework at some level.
Django does have some things going for it that others may lack: a
development community that is active and uses the framework (generally the
development version checked out of
the subversion repository) for live,
high-volume sites, excellent documentation, and a well thought-out design.
For anyone looking for a Python-based web application framework, at least
taking Django for a spin will be time well spent.
(
Log in to post comments)