If any route has been defined within application
configuration, the Pyramidrouter calls a
URL dispatch “route mapper.” The job of the mapper is to
examine the request to determine whether any user-defined
route matches the current WSGI environment. The
router passes the request as an argument to the mapper.
If any route matches, the request is mutated; a matchdict and
matched_route attributes are added to the request object; the
former contains a dictionary representing the matched dynamic
elements of the request’s PATH_INFO value, the latter contains
the IRoute object representing the
route which matched. The root object associated with the route
found is also generated: if the route configuration which
matched has an associated a factory argument, this factory is
used to generate the root object, otherwise a default root
factory is used.
If a route match was not found, and a root_factory argument
was passed to the Configurator constructor, that callable
is used to generate the root object. If the root_factory
argument passed to the Configurator constructor was None, a
default root factory is used to generate a root object.
The Pyramid router calls a “traverser” function with the
root object and the request. The traverser function attempts to
traverse the root object (using any existing __getitem__ on the
root object and subobjects) to find a context. If the root
object has no __getitem__ method, the root itself is assumed to
be the context. The exact traversal algorithm is described in
Traversal. The traverser function returns a
dictionary, which contains a context and a view
name as well as other ancillary information.
The request is decorated with various names returned from the
traverser (such as context, view_name, and so forth), so
they can be accessed via e.g. request.context within
Pyramid looks up a view callable using the context, the
request, and the view name. If a view callable doesn’t exist for this
combination of objects (based on the type of the context, the type of the
request, and the value of the view name, and any predicate
attributes applied to the view configuration), Pyramid raises a
HTTPNotFound exception, which is meant to
be caught by a surrounding exception view.
If a view callable was found, Pyramid attempts to call
the view function.
If an authorization policy is in use, and the view was protected
by a permission, Pyramid passes the context, the request,
and the view_name to a function which determines whether the view being
asked for can be executed by the requesting user, based on credential
information in the request and security information attached to the
context. If it returns True, Pyramid calls the view callable
to obtain a response. If it returns False, it raises a
HTTPForbidden exception, which is meant
to be called by a surrounding exception view.
If any exception was raised within a root factory, by
traversal, by a view callable or by Pyramid itself
(such as when it raises HTTPNotFound or
HTTPForbidden), the router catches the
exception, and attaches it to the request as the exception attribute.
It then attempts to find a exception view for the exception that
was caught. If it finds an exception view callable, that callable is
called, and is presumed to generate a response. If an exception
view that matches the exception cannot be found, the exception is
This is a very high-level overview that leaves out various details.
For more detail about subsystems invoked by the Pyramid router
such as traversal, URL dispatch, views, and event processing, see
URL Dispatch, Views, and