【发布时间】:2014-04-14 23:39:58
【问题描述】:
我花了 4 天多的时间试图弄清楚这一点,但我的每一次尝试都失败了,所以我认为我真的可以使用你的一些帮助。
在下面的代码中,Storm 是一个带有函数submit_data() 的应用程序,它根据实例化时传递的pagename 参数和从Javascript 中的xmlhttp GET 请求传递的idx 参数从数据库中获取数据。
基本上我想要一个带有Root 类的索引页面,以及从XML 文件中读取的具有不同pagename 参数的一堆类似Storm 页面。
我正在使用 RoutesDispatcher,因为我也在使用 XML 文件参数化页面名称(地址)。
当我使用/storm 作为地址时,它甚至不起作用,可能是因为名称冲突,所以我将地址设置为/st/。
现在它显示了标题,但它根本没有找到 submit_data 函数。 Chrome 控制台抛出以下错误。
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/st/submit_data?idx=0
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/st/submit_data?idx=2
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/st/submit_data?idx=1
我该如何构造它,以便当我在浏览器上键入 localhost:8080/st 时,html/javascript 模板 storm.html 会查找 submit_data 函数并调用它并将其显示在网页上?在我开始路由之前,它工作得很好。
class Storm(object):
pagename = ""
def __init__(self, pagename):
self.pagename = pagename
@cherrypy.expose
@cherrypy.tools.mako(filename="storm.html", directories=MEDIA_DIR)
def index(self, name=None):
return {'size': len(params["dashboards"][self.pagename])}
@cherrypy.expose
def submit_data(self, idx):
idx = int(idx)
chart = params["dashboards"][self.pagename][idx]
# Sample page that displays the number of records in "table"
# Open a cursor, using the DB connection for the current thread
c = cherrypy.thread_data.dbdict[chart["dbname"]].cursor()
print 'query being executed......'
c.execute(chart["command"])
print 'query being fetched......'
res = c.fetchall()
c.close()
# construct a JSON object from query result
jsres = []
jsres.append(chart["selected"])
.
.
.
return json.dumps(jsres)
class Root(object):
#storm = Storm("first")
@cherrypy.expose
@cherrypy.tools.mako(filename="index.html", directories=MEDIA_DIR)
def index(self, name=None):
return {}
config = {'/media':
{'tools.staticdir.on': True,
'tools.staticdir.dir': MEDIA_DIR,
}
}
root = Root()
storm = Storm("first")
mapper = cherrypy.dispatch.RoutesDispatcher()
mapper.connect('home','/',controller=root, action='index')
mapper.connect('st','/st/',controller=storm, action='index')
conf = {'/': {'request.dispatch': mapper}}
app=cherrypy.tree.mount(root=None,config=conf)
cherrypy.quickstart(app)
【问题讨论】:
标签: python url routes cherrypy dispatcher