【问题标题】:Integrating CSS and CherryPy: How to fix the 404 "/" Not Found Error?集成 CSS 和 CherryPy:如何修复 404 "/" Not Found 错误?
【发布时间】:2013-08-15 07:28:33
【问题描述】:

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

这是我的文件设置:

  • /TwitApp(应用程序目录)
    • testPy.py(我的测试应用)
    • config.conf(添加CSS的配置文件)
    • global.conf(服务器socket、端口等的全局配置,我觉得这个可以放在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 未找到

    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。有人可以告诉我我做错了什么吗?

【问题讨论】:

    标签: python css http-status-code-404 cherrypy


    【解决方案1】:

    看看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'
    

    【讨论】:

    • 感谢您的提示!我能够通过在我最初使用的编辑器中打开项目来解决这个问题 - vim - 并修复缩进,以便cherrypy.expose不是功能块的一部分。我现在只使用装饰器来避免这个问题(@cherrypy.expose)。 Staticfile 是我遇到的第一个解决方案,所以我使用它并且它有效,但我将在下一次迭代中将其更改为 staticdir。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 2021-01-03
      • 2023-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多