将 CherryPy 与路由调度一起使用

Lar*_*tig 2 python routes cherrypy

我正在尝试将 CherryPy 应用程序从标准 CherryPy 调度切换到 RoutesDispatcher。

以下 python 代码/使用标准 CherryPy 调度正确路由。我的目标是将相同的代码转换为使用 RoutesDispatcher 运行。我已经找到了一些片段,但无法找到使用 Routes 的 CherryPy 应用程序的完整示例。

class ABRoot:  

    def index(self):
        funds = database.FundList()
        template = lookup.get_template("index.html")
        return template.render(fund_list=funds)

index.exposed = True 

if __name__ == '__main__':
    cherrypy.quickstart(ABRoot(), '/', 'ab.config')
Run Code Online (Sandbox Code Playgroud)

我一直在尝试结合我发现的各种部分教程中的代码,但没有任何运气。

我必须__main__对加载和路由进行哪些更改RoutesDispatcher

Lar*_*tig 5

这是我最终开始工作的代码。我需要做出的改变对我来说并不是很明显:

  1. 我不得不将我的配置从文件移动到字典,以便我可以向其中添加调度程序。

  2. 我必须在cherrypy.quickstart 之前调用cherrypy.mount。

  3. 我必须包括 dispatcher.explicit = False

我希望其他处理这个问题的人发现这个答案有帮助。

class ABRoot:  

     def index(self):
         funds = database.FundList()
         template = lookup.get_template("index.html")
         return template.render(fund_list=funds)

if __name__ == '__main__':


     dispatcher = cherrypy.dispatch.RoutesDispatcher()
     dispatcher.explicit = False
     dispatcher.connect('test', '/', ABRoot().index)

     conf = {
    '/' : {
        'request.dispatch' : dispatcher,
        'tools.staticdir.root' : "C:/Path/To/Application",
        'log.screen' : True
    },
    '/css' : {
        'tools.staticdir.debug' : True,
        'tools.staticdir.on' : True,
        'tools.staticdir.dir' : "css"
    },
    '/js' : {
        'tools.staticdir.debug' : True,
        'tools.staticdir.on' : True,
        'tools.staticdir.dir' : "js"
    }
     }

     #conf = {'/' : {'request.dispatch' : dispatcher}}

     cherrypy.tree.mount(None, "/", config=conf) 
     cherrypy.quickstart(None, config=conf)
Run Code Online (Sandbox Code Playgroud)