title: TurboGears
category: page
slug: turbogears
sortorder: 0406
toc: False
sidebartitle: TurboGears
meta: TurboGears is a batteries included web framework that can act both as a full stack or microframework solution, with MongoDB as a first citizen storage.

[TurboGears](http://www.turbogears.org), born as a full stack layer on top
of Pylons, is now a standalone [WSGI](/wsgi-servers.html) web framework
that can act both as a full  stack framework (like [Django](/django.html)) 
or as a micro framework (like [Flask](/flask.html)) 

<a href="http://www.turbogears.org/" style="border: none;">
    <img src="/img/logos/turbogears.jpg" width="100%" alt="TurboGears logo." class="technical-diagram" style="border-radius: 5px;">
</a>

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:

* [Identification and Authentication](http://turbogears.readthedocs.io/en/latest/turbogears/authentication.html)
* [Authorization](http://turbogears.readthedocs.io/en/latest/turbogears/authorization.html)
* [Autogenerated Admin and CRUD](http://turbogears.readthedocs.io/en/latest/cookbook/admin.html)
* [Sessions](http://turbogears.readthedocs.io/en/latest/turbogears/session.html)
* [Caching](http://turbogears.readthedocs.io/en/latest/turbogears/caching.html)
* [Schema Migrations](http://turbogears.readthedocs.io/en/latest/turbogears/migrations.html)
* [Master/Slave Database Queries Balancing](http://turbogears.readthedocs.io/en/latest/cookbook/master-slave.html)
* Request Bound Transactions
* Interactive Debugger
* Builtin Profiling
* Pluggable Applications

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](https://www.youtube.com/watch?v=-QqQVBzU4lM&t=16s)
  An overview of TurboGears2 features presented at the PyConWeb

* [TurboGears Documentation](http://turbogears.readthedocs.io/en/latest/)
  The official TurboGears documentation

* [Microframework Mode Tutorial](http://turbogears.readthedocs.io/en/latest/turbogears/minimal/index.html)
  The official tutorial that focuses on starting TurboGears in microframework
  mode and leads to developement of a single file web application

* [FullStack Tutorial](http://turbogears.readthedocs.io/en/latest/turbogears/wiki20.html)
  The Wiki in 20 minutes tutorial that showcases how to create a fully
  functional wiki application with TurboGears in full stack mode.

* [The CogBin](http://www.turbogears.org/cogbin.html)
  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](https://medium.com/@__amol__/es2015-and-react-in-pure-python-environment-b326dc15012c)
  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](https://talkpython.fm/episodes/show/35/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.
