2013-12-12 114 views
1

我想讓URL映射工作,但到目前爲止還沒有能夠。我附上了config.conf,global.conf和server.py文件,請幫助。CherryPy URL映射不工作

我已經閱讀了關於調度的CherryPy文檔,但我無法抓住我可能做錯事情的地方。我是CherryPy的新手。

我得到以下錯誤:

404 Not Found 

The path '/titles.html' was not found. 
In addition, the custom error page failed: 
ValueError: unsupported format character '"' (0x22) at index 1916 

Traceback (most recent call last): 
    File "/home/dev/fbenavides/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 656, in respond 
    response.body = self.handler() 
    File "/home/dev/fbenavides/lib/python2.7/site-packages/cherrypy/lib/encoding.py", line 188, in __call__ 
    self.body = self.oldhandler(*args, **kwargs) 
    File "/home/dev/fbenavides/lib/python2.7/site-packages/cherrypy/_cperror.py", line 386, in __call__ 
    raise self 
NotFound: (404, "The path '/titles.html' was not found.") 
Powered by CherryPy 3.2.4 

global.conf

[global] 
tools.encode.on:   True 
tools.encode.encoding: 'utf-8' 
tools.decode.on:   True 
tools.trailing_slash.on: True 
tools.staticdir.root: server.HTTP_ROOT 

log.screen:  True 
log.error_file: server.HTTP_ROOT + '/log/errors' 
log.access_file: server.HTTP_ROOT + '/log/access' 

server.socket_host: '0.0.0.0' 
server.socket_port: 8080 

error_page.404: server.HTTP_ROOT + '/templates/404.html' 
error_page.500: server.HTTP_ROOT + '/templates/500.html' 

config.conf

[/] 
tools.staticdir.on: True 
tools.staticdir.debug: True 
tools.staticdir.dir: '' 

[/img] 
tools.staticdir.on: True 
tools.staticdir.debug: True 
tools.staticdir.dir: 'img' 

[/css] 
tools.staticdir.on: True 
tools.staticdir.debug: True 
tools.staticdir.dir: 'css' 

[js] 
tools.staticdir.on: True 
tools.staticdir.debug: True 
tools.staticdir.dir: 'js' 

[templates] 
tools.staticdir.on: True 
tools.staticdir.debug: True 
tools.staticdir.dir: 'templates' 

server.py

# encode: utf-8 
import os 
import cherrypy 
from datetime import date 
from jinja2 import Environment, FileSystemLoader 

HTTP_ROOT = os.path.abspath(os.path.dirname(__file__)) 
env  = Environment(loader=FileSystemLoader(os.path.join(HTTP_ROOT,'templates'))) 

conf = os.path.join(HTTP_ROOT,'config.conf') 
glob = os.path.join(HTTP_ROOT,'global.conf') 

class Root(object): 
    @cherrypy.expose 
    def index(self): 
     tmpl = env.get_template('index.html') 
     return tmpl.render(title='CherryPy - Pythonic WSGI OO HTTP WEB Framework') 

@cherrypy.expose 
def titles(self): 
    today = date.today() 
    tmpl = env.get_template('titles.html') 
    return tmpl.render(launch1=(date(2014,2,18)-today).days) 


root = Root() 
root.titles = titles 

if __name__ == '__main__': 
    cherrypy.config.update(glob) 
    cherrypy.tree.mount(root, "/", config=conf) 
    cherrypy.engine.start() 
    cherrypy.engine.block() 
+0

歡迎來到SO!現在正在發生的實際行爲是什麼? – Derek

回答

1

你的標題句柄就是這樣。

http://localhost/titles 

不需要.html。這隻與您的模板系統的物理路徑有關。 但是,如果你真的要參考什麼:

http://localhost/titles.html 

然後改變你的手柄,這...

@cherrypy.expose 
def titles_html(self): 
    today = date.today() 
    tmpl = env.get_template('titles.html') 
    return tmpl.render(launch1=(date(2014,2,18)-today).days) 

http://docs.cherrypy.org/dev/concepts/dispatching.html#special-characters

希望這有助於!

+0

哎呀!新手失明!非常感謝!!! – Francisco

+0

不客氣。如果我的回答回答了您的問題,請將其標爲接受的答案 - 謝謝! –