2012-03-02 11 views
0

私はCherrypyをウェブサイトに使用したいと思っていますが、私はPythonコードで表示したいページのURLをマッピングする際に問題があります。CherrypyマッピングのURL

今、私はこのコード

#!/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) 

およびコンフィギュレーションファイルはこのあるある:

[/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" 

設定ファイルで指定されたすべてのファイルのためにそこには問題はありませんが、私がアクセスしようとすると、 http://localhost:8080/upload「404が見つかりません」というメッセージが表示される

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.") 

チュートリアル​​に示されているように、私はこの問題を解決するためにさまざまな方法を試しましたが、失敗しました。 私はチュートリアルで報告されていないいくつかの設定が欠落していると思います。

何人か考えてみませんか?
ありがとうございました

答えて

3

私の悪い、私はいくつかの構成が間違っていました。 は、私はこれで解決しました:基本的に私はちょうどルートクラス外の機能を移動し、root.upload = upload

でパスを追加しました

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() 

今では動作します。

関連する問題