gpt4 book ai didi

python - CherryPy URL 映射不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 18:40:48 28 4
gpt4 key购买 nike

我正在尝试使 URL 映射正常工作,但到目前为止还无法实现。我正在附加 config.conf、global.conf 和 server.py 文件,请帮忙。

我已经阅读了有关调度的 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

全局.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'

配置.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'

服务器.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()

最佳答案

你的标题句柄就是这样。

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

希望这有帮助!

关于python - CherryPy URL 映射不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20553495/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com