Reputation: 1988
I'm tying to use Cherrypy for a website, but I have some problem with mapping the url of the page I want to display with function in the python code.
Now I have this code
#!/usr/bin/env python
import os
localDir = os.path.dirname(__file__)
absDir = os.path.join(os.getcwd(), localDir)
import cherrypy
from genshi.template import TemplateLoader
loader = TemplateLoader('../html', auto_reload=True)
class Root(object):
@cherrypy.expose
def index(self):
tmpl = loader.load('index.html')
return tmpl.generate().render('html', doctype='html')
@cherrypy.expose
def upload(self, datafile):
#do something
...
return out % (size, datafile.filename, datafile.content_type)
cherrypy.root.index = index
cherrypy.root.upload = upload
conf = os.path.join(os.path.dirname(__file__), 'server.config')
cherrypy.quickstart(Root(), '/', config=conf)
And the configuration file is this:
[/index.html]
tools.staticfile.on = True
tools.staticfile.filename = "/path-to-file/html/index.html"
[/impemails.html]
tools.staticfile.on = True
tools.staticfile.filename = "/path-to-file/html/impemails.html"
[/css/style.css]
tools.staticfile.on = True
tools.staticfile.filename = "/path-to-file/css/style.css"
[/css/index.css]
tools.staticfile.on = True
tools.staticfile.filename = "/path-to-file/css/index.css"
[/css/imp.css]
tools.staticfile.on = True
tools.staticfile.filename = "/path-to-file/css/imp.css"
For all the file specified in the configuration file there are no problem, but when I try to access upload with the link http://localhost:8080/upload I get the "404 Not found Message"
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/cherrypy/_cprequest.py", line 656, in respond
response.body = self.handler()
File "/usr/local/lib/python2.7/dist-packages/cherrypy/lib/encoding.py", line 188, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/cherrypy/_cperror.py", line 386, in __call__
raise self
NotFound: (404, "The path '/upload' was not found.")
I tried many different ways to solve this problem as shown in the tutorial http://docs.cherrypy.org/dev/concepts/dispatching.html but I failed. I think I'm missing some configurations which are not reported in the tutorial.
Anybody as some ideas?
Thank you in advance
Upvotes: 0
Views: 3734
Reputation: 1988
My bad, I misunderstood some configurations. I've solved with this:
class Root(object):
@cherrypy.expose
def index(self):
tmpl = loader.load('index.html')
return tmpl.generate().render('html', doctype='html')
@cherrypy.expose
def upload(couchdb, maillist, datafile):
return "upload file"
conf = os.path.join(os.path.dirname(__file__), 'server.config')
root = Root()
root.upload = upload
cherrypy.tree.mount(root, '/', config=conf)
cherrypy.engine.start()
cherrypy.engine.block()
Basically I've just moved the function outside the Root class and added the path with root.upload = upload
Now it works.
Upvotes: 3