Search code examples
pythonroutescherrypytrailing-slash

Trailing Slash tool in CherryPy 3.2 not adding a trailing slash


I cannot get the trailing_slash tool to work. I've played around with the options for the trailing_slash tool and nothing seems to have any effect. I am using CherryPy 3.2.2 and Routes 1.13. I would like to have a trailing slash added. There is no errors in the debug output.

If I go to 127.0.0.1:8080/blog/ it works, however if I go to 127.0.0.1:8080/blog it doesn't.

My Configuration is:

conf = {
'/': {
    'request.dispatch': setup_routes(),
    # This trailing slash stuff has no effect :(
    'tools.trailing_slash.on': True,
    'tools.trailing_slash.missing': True,
    'tools.trailing_slash.status': 301,
},
'/static': {
       'tools.staticdir.on': True,
       'tools.staticdir.root': os.path.dirname(os.path.abspath(__file__)),
       'tools.staticdir.dir': 'static'
   }
}

And the example route is:

routes = [["blog_index", "/blog/", {'controller': BlogController(), 'action': 'index','entry_id': 'index'}],]

Can anybody see what I am doing wrong? Is there a bug or lack of essential documentation? Does the trailing_slash tool work with the Routes Dispatcher?

The full src: https://bitbucket.org/ddevine/icdy/src


Solution

  • The trailing slash Tool doesn't work with the Routes Dispatcher. The trailing slash tool expects that whatever dispatcher is chosen will set cherrypy.request.is_index to True or False, which it looks like the RoutesDispatcher class doesn't do at all. Therefore, is_index is left at None, and the trailing slash Tool does nothing. It might be possible for someone with a good knowledge of Routes to fix that in the CherryPy codebase, or write their own dispatcher which sets is_index, or try to write another Tool which fixes up is_index after the fact.