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:
- The more-traditional approach of URL dispatch aka routes
- The more object-oriented approach of :ref:`traversal <pyramid:traversal_chapter>` popularized by Zope
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:
...is nothing more than:
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
We are going to use the previous step as our starting point:
$ cd ..; cp -r layout siteroot; cd siteroot $ $VENV/bin/python setup.py develop
siteroot/tutorial/__init__.py, make a root factory that points to a function in a module we are about to create:
siteroot/tutorial/resources.pywith a class for the root of our site and a factory that returns it:
Our views in
siteroot/tutorial/views.pyare now quite different...no
A template in
A template in
Simple tests in
Now run the tests:
$ $VENV/bin/nosetests tutorial . ---------------------------------------------------------------------- Ran 2 tests in 0.141s OK
Run your Pyramid application with:
$ $VENV/bin/pserve development.ini --reload
http://localhost:6543/helloin your browser.
__init__.py 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
Pyramid provides services beyond simple Python dictionaries. These
services need a little bit more protocol than just
Namely, objects need to provide an attribute/callable for
In this step, our tree has one object: the root. It is an instance of
Root class. The next URL hop is
hello. Our root instance
does not have an item in its dictionary named
so Pyramid looks for a view with a
finding our view method.
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.
- 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?