0

我一直在使用 CherryPy 3.2.2 测试 Twitter Bootstrap,并查看了一些 SO 帖子,但无法成功让 Cherry 使用我的配置文件运行。我收到了臭名昭著的 404 错误:“NotFound: (404, "The path '/' was not found.")”。

这是我的文件设置:

  • /TwitApp(应用程序目录)
    • testPy.py(我的测试应用程序)
    • config.conf(添加 CSS 的配置文件)
    • global.conf (服务器套接字、端口等的全局配置。我认为这可以放在 config.conf 中?)
    • /css(CSS 文件夹)
      • bootstrap.min.css
    • /js(javascript 目录)

这是我的 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 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.")

我阅读最多的文章可以在这里找到:Cherrypy 返回 NotFound: (404, "The path '/' was not found.")和这里:Path Not Found in CherryPy。有人可以告诉我我做错了什么吗?

4

2 回答 2

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'
于 2013-08-21T21:03:00.903 回答
1

最终答案:我决定我应该开始进行故障排除,所以我破解了所有 CherryPy 配置,并cherrypy.quickstart(HelloWorld())使用我拥有的模板进行了基本操作。我仍然收到 404 错误,这意味着我的代码存在根本性错误,而不是配置错误。我一直在寻找所有东西,然后我找到了它。在 vim 上,我的代码对齐有点偏离,那时我注意到它index.exposed = True没有设置在 index 函数之外!哦!我替换了它,现在它可以使用 CSS,所以任何有兴趣学习如何使用 CSS 实现 CherryPy 的人,都可以!

于 2013-08-13T23:38:34.227 回答