To configure routing, you have to first include this package in your pyramid app:


And add routing_package into you settings ini file. This setting should pinpoint the package containing routing.

    # ....
    routing_package = my.package.lib.routing
    # ....

If You use routing package in other pyramid module, you need this package via normal import:

from tzf.pyramid_routing import routes_from_package
routes_from_package(config, 'module.lib.routing')


Depending on the order of including plugins, you should add config.commit() between pyramid_routing inclusion, and the other module, that uses it.

Although, It doesn’t give errors in all cases. Investigation is needed.

Defining routes

By default, routing should be defined in package’s module and it’s submodules in lists of dicts. lists should be named routes, and the dictionary format is the same one as pyramid’s config.add_route‘s method. routes definition is read as is, and for convenience, is loaded at the end. This gives the opportunity to add routes catching all possibilities: /{match}

Module-defined routes will be loaded first, with module name as their prefix. If will contain:

routes = [
    {'name': 'blog:index', 'pattern':'/'},
    {'name':'blog:show', 'pattern':'/show'},

blog:index path will be /blog/, while blog:show will become /blog/show

Redefining route prefix

Route prefix for routing submodules reflects the submodule name. But if you require some more fancy name (Possibly a variable, that is beeing set by default), you can do so, by defining a prefix variable on that module:

prefix = '{locale}/blog'

routes = [
    {'name': 'blog:index', 'pattern':'/'},
    {'name':'blog:show', 'pattern':'/show'},

blog:index path now will be {locale}/blog/, while blog:show will become {locale}/blog/show. Of course, the /blog part in prefix can be totally missed.

Table Of Contents

Previous topic


This Page