问题描述
我一直在使用CherryPy 3.2.2测试Twitter Bootstrap,并且已经审查了几个SO帖子,但是无法成功地使用我的配置文件运行。我得到臭名昭着的404错误:NotFound:(404,路径'/'没有找到。)。
I've been working on testing Twitter Bootstrap with CherryPy 3.2.2, and have reviewed several of the SO posts, but have been unable to successfully get Cherry to run with my configuration files. I'm getting the infamous 404 error: "NotFound: (404, "The path '/' was not found.")".
这是我的文件设置:
- / TwitApp b $ b
- testPy.py(我的测试应用程序)
- config.conf(用于添加CSS的配置文件)
- / css(CSS文件夹)
ul> - bootstrap.min.css
- /TwitApp (application directory)
- testPy.py (my test application)
- config.conf (configuration file to add CSS)
- global.conf (global configuration for server socket, port, etc. I think this can go in config.conf?)
- /css (CSS folder)
- bootstrap.min.css
这是我的testPy.py代码:
This is my testPy.py code:
#!/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文件是我的config.conf文件的顶部,但是当我开始使用mount,start和block方法时,我把它分成两部分。
At one point the [global] section of my global.conf file was the top section of my config.conf file, but I split the two up when I started using the mount, start and block methods.
这是我的global.conf文件:
Here is my global.conf file:
[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"
$ b b这是我的config.conf文件:
Here is my config.conf file:
[/] tools.staticfile.root = "/Users/myUser/tmp/newproject/TwitApp" [/css/bootstrap.min.css] tools.staticfile.on = True tools.staticfile.filename = "css/bootstrap.min.css"
这是完整的错误信息:
404找不到This is the full error message: 404 Not Found
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.")
我通过阅读的文章可以在这里找到:。有人可以告诉我,我做错了吗?
The articles I read through the most can be found here: Cherrypy returning NotFound: (404, "The path '/' was not found.") and here: Path Not Found in CherryPy. Can someone please advise what I'm doing incorrectly?
推荐答案
最终答案:我决定我应该开始排除故障,所以我黑掉了所有的CherryPy配置,基本
cherrypy.quickstart(HelloWorld())
与我的模板。我仍然得到404错误,这意味着有一些根本上我的代码错误,而不是配置错误。我一直看着一切,然后我发现它。在vim上,我的代码对齐有点偏离,这是当我注意到,index.exposed = True
没有设置索引函数之外!噢!我替换它,现在它使用CSS,所以任何人谁有兴趣学习如何实现CherryPy与CSS,有ya去!Final answer: I decided that I should just start troubleshooting, so I hacked down all of the CherryPy configurations and just did a basic
cherrypy.quickstart(HelloWorld())
with the template I had. I was still getting the 404 error, so that meant that there was something fundamentally wrong with my code, not a config error. I kept looking through everything and then I found it. On vim, my code alignment was a little off and that's when I noticed thatindex.exposed = True
was not set outside the index function! D'oh! I replaced it and now it's working with CSS, so anyone who's interested in learning how to implement CherryPy with CSS, there ya go!这篇关于集成CSS和CherryPy:如何修复404“/”找不到错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!