Reputation: 2705
I have spent more than 4 days trying to figure out this, but my every attempt failed, so I thought I could really use some of your helps.
In the code below, Storm
is an app with function submit_data()
which fetches data from databases according to pagename
parameter passed at instantiation and idx
parameter passed from xmlhttp GET request in Javascript.
Basically I want to have an index page with Root
class, and a bunch of similar Storm
pages with different pagename
parameters read from an XML file.
I am using RoutesDispatcher because I am parametrizing page name(address) using the XML file too.
When I used /storm
as the address it didn't even work probably because the name clashes, so I set the address as /st/
.
Now it displays the header, but it does not find submit_data function at all. The Chrome console throws the following error.
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
How can I structure this so that when I type localhost:8080/st on my browser, the html/javascript template storm.html
looks up submit_data
function and call it and display it on the webpage? Before I started routing, it worked totally fine.
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)
Upvotes: 0
Views: 221
Reputation: 57338
mapper.connect('st','/st/submit_data',controller=storm, action='submit_data')
Routes explicitly list all their handlers, and because of that also don't need to be exposed. Based on this example, you might not want the routes dispatcher. The default dispatcher with multiple roots mounted would work fine.
Upvotes: 1