Integrating CSS and CherryPy: How to fix the 404 “/” Not Found Error?

混江龙づ霸主 提交于 2019-12-13 04:37:51

问题


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.")".

This is my file setup:

  • /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
    • /js (javascript directory)

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()

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.

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"

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"

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?


回答1:


Take a look at https://github.com/btsuhako/bootstrap-cherrypy-jinja2

In the configuration files, it's easier to setup a static dir in your CherryPy app config file:

[/css]
tools.staticdir.on: True
tools.staticdir.dir: 'css'

[/js]
tools.staticdir.on: True
tools.staticdir.dir: 'js'



回答2:


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 that index.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!



来源:https://stackoverflow.com/questions/18213197/integrating-css-and-cherrypy-how-to-fix-the-404-not-found-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!