Routes Application

Since the 0.9.8 version of colubrid there is a application called RoutesApplication which uses the routes URL mapper.

This application type requires an installed routes version.

Here a small example of how to use it:

from colubrid import RoutesApplication, HttpResponse, execute

class Blog(object):

    def index(self):
        url = self.request.link_to(Blog.entry, id=25)
        caption = 'click here to go to the blog entry #25'
        return HttpResponse('<a href="%s">%s</a>' % (url, caption))

    def entry(self, id):
        return HttpResponse('Entry #%s' % id)

    def archive(self, year=None, month=None):
        return HttpResponse('Archive %s/%s' % (year, month))

class app(RoutesApplication):
    mapping = [
        ('/', Blog.index),
        ('/entry/:id', Blog.entry, {'id': r'\d+'}),
        ('/archive/:year/:month', Blog.archive)
    ]

if __name__ == '__main__':
    execute(app)

The RoutesApplication attaches a method called link_to on the request object. You have to use this method as a replacement for the original url_for function which has some problems with the threading and can't resolve the handler references.

The mapping list takes tuples which have to look like this:

route_path the path with named parameters. route_path required
handler a reference to a class method.   required
requirements a dict of regular expressions which matches the parameters. requirements optional
defaults default values. defaults optional
Last Change: 10 Sep 2006 17:12:26 | show source