TurboGears

TurboGears, born as a full stack layer on top of Pylons, is now a standalone WSGI web framework that can act both as a full stack framework (like Django) or as a micro framework (like Flask)

TurboGears logo.

Originally inspired by RubyOnRails it's based on MVC where the controller dispatches the request to a set of actions exposed from the controller itself.

TurboGears, in its full stack mode, provides all the features you would require during development of a web application:

It's also one of the few web frameworks officially supporting MongoDB as one of the primary storage backends, including support into the TurboGears Admin to autogenerate CRUDs from MongoDB models.

While TurboGears has always been a full stack framework with same scope of projects like Django, it differentiates from other frameworks due the its philosophy on two major parts of a web framework: Templating and Routing

Templating

While TurboGears provides support for multiple template engines, the primary one has always been a fully validated XML template engine.

Currently TurboGears ships with the Kajiki template engine, which was developed within the project itself, but in the past it relied on the Genshi and Kid template engines which were mostly syntax compatible with Kajiki.

Historically validated xml template engines has always been slower than text template engines, but the Kajiki project was able to create a very fast template engine that usually renders faster than Mako or Django Template while still retaining all the expected features.

The fact that it relies on a validated XML engine provides some benefits compared to plain text engines like Django Template, Jinja2 and Mako:

Automatic Escaping

It automatically escapes content rendered into the template, thus making easier to avoid XSS and injection security issues:

<div>${value}</div>

with value='<script>alert("hello")</script>' will render as

<div>&lt;script&gt;alert(&quot;hello&quot;)&lt;/script&gt;</div>;

thus preventing any form of injection from user provided content.

Automatic Internationalization

The template engine parses the provided template document and recognises the nodes that contain static text.

As the engine is able to distinguish text from markup it's able to flag the text for translation.

Content like <div>Hello World</div> would get automatically translated if a translation for "Hello World" is provided, without having to wrap text in gettext calls.

Compatibility with WYSIWYG Editors

As the template engine syntax is purely valid XHTML the template itself can be opened with WYSIWYG editors and as far as they don't strip unknown attributes the template can be edited and saved back from those editors.

Routing

Most web frameworks have been relying on regular expressions to declare routing, through decorators or through a routing map.

TurboGears supports regular expressions through the tgext.routes extension, but the preferred way of routing is through the Object Dispatch system.

In Object Dispatch a root controller object is traversed while resolving the URL. Each part of the url path is mapped to a property of the controller (Which might point to a sub controller) until a final callable action is encountered.

This leads to a very natural mapping between URLs and the code serving them, allowing people with minimal knowledge of a project to jump in and quickly find actions in charge of serving a specific page.

In Object Dispatch an URL like /users/new?name=MyName would be served by a hierarchy of objects like:

class UsersController(TGController):
    @expose()
    def new(self, name=None):
       return 'Hi, %s' % name

class RootController(TGController):
    users = UsersController()

It's easy to see how /users/new actually resolves to RootController.users.new and all options provided to the URL are passed to the action serving the response as arguments.

TurboGears Resources

  • TurboGears Introduction Video An overview of TurboGears2 features presented at the PyConWeb

  • TurboGears Documentation The official TurboGears documentation

  • Microframework Mode Tutorial The official tutorial that focuses on starting TurboGears in microframework mode and leads to developement of a single file web application

  • FullStack Tutorial The Wiki in 20 minutes tutorial that showcases how to create a fully functional wiki application with TurboGears in full stack mode.

  • The CogBin The CogBin is a list of the most common pluggable applications for TurboGears, it enlists ready made pieces you can plug into your web application to provide features like Facebook Login, Comments, Registration and so on...

  • React in Pure Python An article showcasing how to create web applications relying on React without the need to have NodeJS installed at all. The article uses TurboGears as the web framework to develop the example application.

  • Turbogears and the future of Python web frameworks is a Talk Python to Me podcast episode featuring an interview with one of the TurboGears core developers.

What do you want to learn next?

I've built a Python web app, now how do I deploy it?

My app runs but looks awful. How do I style the user interface?

What other Python web frameworks exist?


Matt Makai 2012-2022