2011-10-05 45 views
2

我試圖從標準CherryPy調度切換到RoutesDispatcher的CherryPy應用程序。使用CherryPy調度路由

以下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') 

我徘徊在試圖結合我發現沒有任何運氣的各種部分教程的代碼。

我必須對__main__進行哪些更改才能通過RoutesDispatcher加載和路由?

回答

3

這是我最終得到的代碼。我需要修改,使那些不立即明顯對我說:

  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) 
+1

「dispatcher.explicit = False」行的用途是什麼?在試圖解決升級到CherryPy 3.2的問題時,我已經看到了這個問題,但是它應用於映射器「dispatcher.mapper.explicit = False」。上面的代碼似乎在沒有這條線的情況下也是一樣的。 – EmmEff