我一直在使用CherryPy 3.2.2测试Twitter Bootstrap,并已经审查过几个SO帖子,但是无法成功让Cherry运行我的配置文件。我得到臭名昭着的404错误:“NotFound:(404,”未找到路径'/'。)“。
这是我的文件设置:
这是我的testPy.py代码:
#!/usr/bin/env python
import cherrypy
class HelloWorld:
def index(self):
return '''<!DOCTYPE html><html><head>
<title>Bootstrap Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="../css/bootstrap.min.css" rel="stylesheet" media="screen">
</head><body>
<h1>Bootstrap Test</h1>
<button type="button" class="btn">Submit</button>
</body></html>'''
index.exposed = True
#I tried this example that I found in the documentation and on previous posts with the same 404 result:
#cherrypy.quickstart(HelloWorld(), "/", "config.conf")
#I tried this one when I reviewed another post on here:
cherrypy.config.update("global.conf")
cherrypy.tree.mount(HelloWorld(),"/","config.conf")
cherrypy.engine.start()
cherrypy.engine.block()
有一次,我的global.conf文件的[global]部分是我的config.conf文件的顶部,但是当我开始使用mount,start和block方法时,我将这两部分分开了。
这是我的global.conf文件:
[global]
server.socket_host = "127.0.0.1"
server.socket_port = 8080
log.screen: True
log.error_file: "/Users/myUser/tmp/newproject/cherrypy.error"
log.access_file: "/Users/myUser/tmp/newproject/cherrypy.access"
这是我的config.conf文件:
[/]
tools.staticfile.root = "/Users/myUser/tmp/newproject/TwitApp"
[/css/bootstrap.min.css]
tools.staticfile.on = True
tools.staticfile.filename = "css/bootstrap.min.css"
这是完整的错误消息: 找不到404
The path '/' was not found.
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cprequest.py", line 656, in respond
response.body = self.handler()
File "/Library/Python/2.7/site-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/lib/encoding.py", line 188, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "/Library/Python/2.7/site-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cperror.py", line 386, in __call__
raise self
NotFound: (404, "The path '/' was not found.")
我在这里阅读的文章最多可以在这里找到:Cherrypy returning NotFound: (404, "The path '/' was not found.")和此处:Path Not Found in CherryPy。有人可以告诉我我做错了什么吗?
答案 0 :(得分:3)
查看https://github.com/btsuhako/bootstrap-cherrypy-jinja2
在配置文件中,在CherryPy应用配置文件中设置静态目录更容易:
[/css]
tools.staticdir.on: True
tools.staticdir.dir: 'css'
[/js]
tools.staticdir.on: True
tools.staticdir.dir: 'js'
答案 1 :(得分:1)
最后的回答:我决定我应该开始排除故障,所以我已经破解了所有的CherryPy配置,只是使用我的模板做了一个基本的cherrypy.quickstart(HelloWorld())
。我仍然得到404错误,所以这意味着我的代码存在根本性的错误,而不是配置错误。我一直在浏览所有内容然后找到了它。在vim上,我的代码对齐有点偏离,当我注意到index.exposed = True
没有设置在索引函数之外时! D'哦!我替换了它,现在它正在使用CSS,所以任何有兴趣学习如何使用CSS实现CherryPy的人都可以去了!