Introducing Three Python Web Frameworks

Take a quick look at three Web framework technologies from the Python community.

What with all the hoopla surrounding Ruby on Rails, aspiring Web application programmers should be forgiven for assuming that Rails is the only way to go when building their next great Web app. But, Rails is not the only game in town, and Ruby is not the only language worth considering in this very active area. Perl has a large and growing collection of Web frameworks, including Maypole, Catalyst, Jifty and Gantry (to name a few). Even in the Ruby space, the list of technologies is growing to include Merb, Camping, Nitro/Og and Ramaze. And, let's not forget some of the projects from the world's favorite Web templating language, PHP: Zoop, Jelix, Cake and Biscuit.

In fact, entering the name of your favorite programming language into Google together with the words “web framework” produces lists that seem to go on and on. In all the noise, it is easy to forget the other great scripting language, Python, which steadily has been growing in popularity. And deservedly so—Python is a great language, and among its many accolades, it won the Favorite Scripting Language category in the 2008 LJ Readers' Choice Awards and the Favorite Programming Language in 2009.

In this article, I introduce three of the most active Web frameworks for Python: Django, Pylons and TurboGears.

Two technologies that I do not include in this article, and that some readers may feel warrant discussion, are Zope and web2py. At the time of this writing, Zope is primarily used to build content management system Web sites, and web2py looks very promising. Both Zope and web2py may suit your needs, and you may want to explore them further (see Resources).

Let's Meet the Frameworks

Django was developed as a result of work performed at the World Online news site. As a result, it claims to be rock solid, scalable and adaptable to many uses. Pylons is heavily influenced by the design of Ruby on Rails, and it aims to provide the best bits from a collection of other Python frameworks. It is a lightweight framework, built for speed. TurboGears, billed as a meta-framework, is built upon a collection of other tools, including the SQLObject database layer, the cherrypy HTTP middleware technology, the Kid templating system and the MochiKit AJAX library. All three of these Web frameworks follow the now-familiar Model-View-Controller paradigm, which rapidly is becoming the defining characteristics of all Web framework technologies.


All three frameworks are available within the Ubuntu repositories. However, at the time of this writing, I found the versions supported by the repositories to be somewhat older than the latest-and-greatest release from each of the framework's Web sites. Consequently, I opted to perform a download/install as opposed to an apt-get for each framework. Thankfully, installing all three of these frameworks is a breeze—assuming, of course, that Python already is installed on your computer. As Python has been a standard component within most distributions for many years now, this isn't really an issue. Note that I had to install the python-mysqldb package from the Ubuntu repositories in order to work with MySQL, my chosen Web database back end. At the time of writing, the versions of the frameworks I worked with were TurboGears 1.0.7, Pylons and the 1.0 release of Django.

To get a copy of Django, download the latest tarball from the Django Web site (see Resources), then issue the following commands at the GNU/Linux prompt:

tar -zxvf Django-1.0.tar.gz
cd Django-1.0
sudo python install

For Pylons, use this:

easy_install Pylons==

For TurboGears, download the script from the TurboGears Web site (see Resources), then issue the following command at the GNU/Linux prompt:

sudo python

And, that's it! You now are ready to explore each framework.

Getting Your Feet Wet

Each framework provides comprehensive on-line documentation designed to get Web developers up and running as quickly as possible. I'm going to review each of the frameworks based on their “get up to speed quickly” tutorials. After all, if Web developers can't gain experience in a Web framework quickly, they likely will move on to another, so the quality of the tutorial is important.


The Django tutorial is an excellent step-by-step guide designed to get a simple Django project off the ground as quickly as possible. I liked the fact that the tutorial provided lots of links to more detailed documentation but did not require you to read it in order to follow along. In no more than a few minutes, I had a server running, and Django was serving up its default Web page.

There are four parts to the Django tutorial, which walks you through creating a Web-based polling application. The first part prepares the database to hold the Web application's data, which tracks poll questions and the votes associated with them. In doing so, you are introduced to the Django admin utility. This part of the tutorial walks you through a series of shell interactions with the database, using the Django database API, clearly demonstrating the methods—automatically created by Django—that can be used to interact with the Web application's data. As with other frameworks, there's no real need to know SQL, as Django abstracts and automates most of the database interactions.

With the data side ready, Part 2 of the tutorial concentrates on the Web application's admin side, which is designed to be used by Web site managers. As with Part 1, the instructions are of the follow-along type, clearly written and straightforward. The functionality provided by the Django admin system is impressive to say the least. The tutorial itself suggests you “take a few minutes to marvel at all the code you didn't have to write”. There's buckets of administrative functionality provided for free here. The tutorial provides a quick run though the tiny amounts of code that can be added to the Web application to control the look and feel of the administrative interface. For instance, if you want some fancy AJAX features, it's a trivial change.

Part 3 presents the Django code necessary to build the public interface of the sample Web application. The first attempt at providing a view uses Python code to generate content that works, but breaks really quickly when changes are required on a regular basis. As a consequence, Django provides for the design of a Web page to be kept separate from the Python controller code, using its own templating system, which is a mix of HTML and Django control logic (enclosed within {% and $} tags). Here's an example taken from the tutorial:

{% if latest_poll_list %}
    {% for poll in latest_poll_list %}
        <li>{{ poll.question }}</li>
    {% endfor %}
{% else %}
    <p>No polls are available.</p>
{% endif %}

When displayed, this template displays the list of polls as a bullet list. Note how the Django logic code (inside the {% and %} delimiters) is written in a Python-like language, providing a means by which the display can be controlled based on the availability of any data.

The final part of the Django tutorial completes the example application by providing the Python code and templates required to polish off the Web application. This includes some simple form processing as well as advice on factoring and reusing code. It also introduces the powerful concept of Django's generic views, which are designed to reduce radically the amount of Python code that Django programmers need to write.

I spent a few hours working through the entire Django tutorial, playing with the example code and reading some of the related material. All in all, the Django tutorial was a joy to work through, and I often found myself shaking my head at just how easy some of it was when compared to the complex functionality provided. The tutorial not only gets you up and running quickly with a useful Django project, but it also whets your appetite for more. A nice feature, which it shares with other such systems, is that as you make changes to your project's source files, the Django Web server automatically reloads the Web application to make the changes immediately available for testing. Although such a setup would be horribly slow within a production setting, it is a godsend when developing. It is also somewhat cool to edit a file in one terminal window, save it, and then have the Django server automatically restart in another terminal window as a result of the state change.


Having successfully installed Pylons, I dived into the “Getting Started” section of the on-line documentation and started working. Almost immediately, I hit a small snag when Pylons complained that my Python did not have a profiler module. A quick apt-get fixed that:

sudo apt-get install python-profiler

The Pylons tutorial walks you through a simple HelloWorld application. Before even discussing any code, the tutorial states that any finished Pylons applications can be distributed using the Python EGG package format, which is cool. When a new project is created, Pylons creates the necessary infrastructure to enable the creation of an EGG with ease. A quick look at the files created for a new project reveals that Pylons is indeed inspired by Ruby on Rails (and, of course, there's nothing wrong with that). One of the pluses of this design decision is that testing is right up there as far as Pylons is concerned, with a tests directory created automatically for each new project. By making testing a no-brainer, Pylons makes it hard to not consider testing when building a Web application.

Working through the creation of the sample application was a little disconcerting, as Pylons felt so much like Ruby on Rails. The Pylons routing system is almost an exact clone. That said, the Pylons templating system is its own and, by default, is based on Mako (see Resources). Mako is a templating technology that, like Django, provides a Python-like syntax, and the Mako and Pylons projects are very closely related.

Unlike Django, which provides a clear, logical tutorial designed to get an application up and running quickly, the Pylons equivalent is somewhat more threadbare, and it assumes that the reader has some experience with Web application development (and frameworks). This may work for some, but not all. For instance, I was a little disappointed not to see any mention of database connectivity within the introductory material. The Pylons documentation does talk about SQLAlchemy, which is a separately downloadable add-on that abstracts a collection of popular database management systems. I expected this technology to work out of the box and felt a little let down when it didn't. To be fair to Pylons, at the time of this writing, it is not yet at a 1.0 release (unlike Django and TurboGears), so some of the rough edges may be smoothed out by the time you read this. Certainly, what's included already shows great promise, and the frameworks claim that it is fast was borne out by the testing that I conducted.

Another likeable characteristic of Pylons is that it has been built to be customizable. Don't like Mako? That's okay, as Pylons supports a growing collection of other templating technologies. The same goes for SQLAlchemy. Pylons also supports SQLObject as well as Python's standard DB-API. This makes Pylons very flexible (as there's all this choice) and certainly makes it a Web framework to watch.


TurboGears is built on top of a collection of Web development tools in much the same way that Pylons is. Of considerable interest is the fact that the next major release, TurboGears 2.0, is designed to sit on top of, among other technologies, Pylons! What the TurboGears community promises is the ability to work at a higher and more efficient level—hence, the idea of a meta-framework.

The official tutorial is the “20 Minute Wiki”, which is available as a screencast as well as a Web-based document. Don't be tempted to try to follow along with the screencast (as I was). It moves along far too fast to be anything other than a quick overview. The written tutorial covers the same content as the screencast, but it is designed to be worked through. Like the Django tutorial, this one is well written and easy to follow. Like Pylons, TurboGears provides the bits and pieces required to create EGGs as well as write tests for your new project. Unlike Pylons, but like Django, the TurboGears tutorial is based around storing data in a database, and SQLite is used.

Plenty of instructions are provided for configuring TurboGears with an alternative database back end, so I configured TurboGears to work with MySQL (it was a trivial one-line change to a config file). When I tried to create my database schema with the suggested command, I was greeted with a long list of Python exception-handling errors. Figuring that I'd made a mistake with my MySQL configuration, but not able to identify it, I dropped back to using SQLite only to have the same error. At this point, I actually read the error messages in detail and discovered that the 0.9.1 release of SQLObject installed on my system needed to be the 0.10.1 release. When I used easy_install to check the version of SQLObject installed, it dutifully informed me that 0.9.1 was installed and that this was my “best option”. The following command forced easy_install to use the latest-and-greatest development version of SQLObject, which is 0.10.1:

sudo easy_install -U SQLObject

With that out of the way, the creation of the database schema worked without issue. Next up was creating a template using Kid for the Wiki. Of note is that Kid allows Python to be used as the templating language (as opposed to Django's practice of providing a Python-like templating language). A nice feature of the tutorial is the fact that programmers working through each page can comment and have their comments included at the bottom of the page. This allows for comments along the lines of “don't forget to try this”, or “a better technique is this” and so on. There's always lots to be learned from other programmers' experiences, making the comments at the bottom of the TurboGears tutorial very beneficial. It is a shame that the commenting has been disabled due to heavy spamming (hopefully this restriction will be lifted soon).

Another great feature is the TurboGears Toolbox. This is a Web-based collection of tools for working with various parts of your TurboGears Web application. The tutorial walks through using the Toolbox to add some data to the database with the CatWalk tool. It's all point and click and very slick. The tutorial progresses through the creation of a reasonably functioning Wiki, with AJAX included. At the end of the tutorial, there are plenty of suggestions for further reading. Although the tutorial is called the “20 Minute Wiki”, which is a little misleading. Granted, the screencast lasts 20 minutes, but plan to dedicate a few hours to the task when working through the tutorial on your own.

Learning More

In addition to the on-line documentation provided by each of the framework's Web sites, a growing collection of books is dedicated to the Python Web application space. Typing the name of each framework into uncovered five titles for Django, one title for TurboGears and one for Pylons. There's also a small number of books that provide an overview of Python Web frameworks and include material on some or all of the frameworks mentioned here.


Python programmers who are of the opinion that Ruby on Rails is the only game in town when it comes to Web application development needn't fret. The Python space provides lots of options for budding Web developers who want to apply already-acquired and hard-won Python skills to the Web platform. Of course, it's not just Python programmers who can gain here. Anyone looking to get into Web development is advised to look around. Ruby on Rails is cool, but it's not for everyone. Plenty of choices exist, and Python has its fair share of worthy contenders. Django and TurboGears are the established players in this area. However, as those of us in the GNU/Linux community well know, the young upstart can often cause a splash, and Pylons may well do just that. If you like the look of Ruby on Rails, but prefer Python, Pylons is very much worth spending time with. If I were starting a new project, I'd be hard pressed to pick one of these three frameworks, as they all have merit. However, I liked the feel of TurboGears and the fact that future development is to be based on Pylons, which makes me think that I'd be enjoying the best of both worlds—a very attractive proposition. All three are more than good enough to keep Web developing Python programmers away from Ruby on Rails.








Paul Barry ( lectures at the Institute of Technology, Carlow in Ireland. Find out more about the stuff he does at his Web site:

Load Disqus comments