【问题标题】:Using CherryPy with Routes dispatching将 CherryPy 与路由调度一起使用
【发布时间】:2011-10-05 03:02:51
【问题描述】:

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

以下 python 代码使用标准 CherryPy 调度正确路由 /。我的目标是将相同的代码转换为使用 RoutesDispatcher 运行。我已经找到了 sn-ps,但找不到使用 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')

我一直在尝试组合来自各种部分教程的代码,但没有任何运气。

我必须对__main__ 进行哪些更改才能通过RoutesDispatcher 加载和路由?

【问题讨论】:

    标签: python routes cherrypy


    【解决方案1】:

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

    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)
    

    【讨论】:

    • “dispatcher.explicit = False”行的目的是什么?在尝试解决升级到 CherryPy 3.2 的问题时,我看到了对此的引用,但是它被应用于映射器“dispatcher.mapper.explicit = False”。上面的代码似乎在有和没有这条线的情况下都一样。
    猜你喜欢
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 2017-01-03
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多