Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

159 lines (106 sloc) 4.734 kb

2: Basic Traversal With Site Roots

Model websites as a hierarchy of objects with operations.


Web applications have URLs which locate data and make operations on that data. Pyramid supports two ways of mapping URLs into Python operations:

In this section we will introduce traversal bit-by-bit. Along the way, we will try to show how easy and Pythonic it is to think in terms of traversal.

Remember...traversal is easy, powerful, and useful.

With traversal, you think of your website as a tree of Python objects, just like a dictionary of dictionaries. For example: nothing more than:

>>> root['aFolder']['subFolder'].search()

To remove some mystery about traversal, we start with the smallest possible step: an object at the top of our URL space. This object acts as the "root" and has a view which shows some data on that object.


  • Make a factory for the root object
  • Pass it to the configurator
  • Have a view which displays an attribute on that object


  1. We are going to use the previous step as our starting point:

    $ cd ..; cp -r layout siteroot; cd siteroot
    $ $VENV/bin/python develop
  2. In siteroot/tutorial/, make a root factory that points to a function in a module we are about to create:

  3. We have siteroot/tutorial/ with a class for the root of our site and a factory that returns it:

  4. Our views in siteroot/tutorial/ are now quite route_name:

  5. A template in siteroot/tutorial/templates/home.jinja2:

  6. A template in siteroot/tutorial/templates/hello.jinja2:

  7. Simple tests in siteroot/tutorial/

  8. Now run the tests:

    $ $VENV/bin/nosetests tutorial
    Ran 2 tests in 0.141s
  9. Run your Pyramid application with:

    $ $VENV/bin/pserve development.ini --reload
  10. Open http://localhost:6543/hello in your browser.


Our has a small but important change: we create the configuration with a root factory. Our root factory is a simple function that performs some work and returns the root object in the :ref:`resource tree <pyramid:the_resource_tree>`.

In the resource tree, Pyramid can match URLs to objects and subobjects, finishing in a view as the operation to perform. Traversing through containers is done using Python's normal __getitem__ dictionary protocol.

Pyramid provides services beyond simple Python dictionaries. These :ref:`location <pyramid:location_aware>` services need a little bit more protocol than just __getitem__. Namely, objects need to provide an attribute/callable for __name__ and __parent__.

In this step, our tree has one object: the root. It is an instance of our Root class. The next URL hop is hello. Our root instance does not have an item in its dictionary named hello, so Pyramid looks for a view with a name=hello, finding our view method.

Our home view is passed, by Pyramid, the instance of this folder as context. The view can then grab attributes and other data from the object that is the focus of the URL.

Now, on to the most visible part: no more routes! Previously we wrote URL "replacement patterns" which mapped to a route. The route extracted data from the patterns and made this data available to views that were mapped to that route.

Instead, segments in URLs become object identifiers in Python.

Extra Credit

  1. Is the root factory called once on startup, or on every request? Do a small change that answers this. What is the impact of the answer on this?
Jump to Line
Something went wrong with that request. Please try again.