我想致电http://localhost:8080/match/123/subtitle
def match(self, dayid):
day = dayid
day_folder = os.path.join(root_folder,day)
#Some logics here
@cherrypy.expose
def subtitle(self, mid):
return "Requesting subtitle for ", mid
这不是正确的方法吗?如果没有,重写Url的正确方法是什么?
我尝试了以下但是它也不起作用。
d = cherrypy.dispatch.RoutesDispatcher()
d.connect('default_route', '', controller=HelloWorld)
d.connect('subtitle', 'match/{mid}/subtitle', controller=HelloWorld, action='subtitle')
cherrypy.config.update({{'request.dispatch': d}})
答案 0 :(得分:1)
您可以使用自定义调度程序执行此操作。这是一个简单的例子,可以给你一个想法。你在这里走在正确的轨道上。
import cherrypy
class my_index_controller:
@cherrypy.expose
def index(self):
return """<h1>WORKS</h1>"""
class other_controller:
@cherrypy.expose
def subtitle(self, mid):
return """<h1>WORKS2 %s</h1>""" % str(mid)
controller1 = my_index_controller()
controller2 = other_controller()
d = cherrypy.dispatch.RoutesDispatcher()
d.connect('index', '/', controller=controller1, action='index')
d.connect('subtitle', '/match/:mid/subtitle', controller=controller2, action='subtitle')
conf = {'/': {'request.dispatch': d}}
if __name__=='__main__':
cherrypy.quickstart(controller1, '/', config=conf)