Routes Dispatcher示例在Cherrypy中不起作用

时间:2012-07-31 11:10:58

标签: python routes dispatcher cherrypy

我尝试了在cherrypy essentials中的Routes Dispatcher示例,但它不起作用。它给我Page not found错误。我错过了什么?

import cherrypy
class Root:
    def index(self):
        return "Not much to say"
    def hello(self, name):
        return "Hello %s" % name
if __name__ == '__main__':
    root = Root()
    # Create an instance of the dispatcher
    d = cherrypy.dispatch.RoutesDispatcher()
    # connect a route that will be handled by the 'index' handler
    d.connect('default_route', '', controller=root)
    # connect a route to the 'hello' handler
    # this will match URIs such as '/say/hello/there'
    # but not '/hello/there'
    d.connect('some_other', 'say/:action/:name',
    controller=root, action='hello')
    # set the dispatcher
    conf = {'/': {'request.dispatch': d}}
    cherrypy.quickstart(root, '/', config=conf)

2 个答案:

答案 0 :(得分:3)

我知道这个问题早已解决了,但我认为值得注意的是,我能够通过在连接路径中添加起始斜杠来修复无法正常工作的路由:

d.connect('some_other', 'say/:action/:name',

d.connect('some_other', '/say/:action/:name',

希望这有助于某人。

答案 1 :(得分:-1)

尝试暴露处理程序......

 @cherrypy.expose
 def index(self):
    return "Not much to say"
 @cherrypy.expose
 def hello(self, name):
    return "Hello %s" % name

安德鲁