How do I make a trailing slash optional with webapp2?

后端 未结 9 847
感情败类
感情败类 2021-02-12 15:30

I\'m using the new webapp2 (now the default webapp in 1.6), and I haven\'t been able to figure out how to make the trailing slash optional in code like this:

web         


        
9条回答
  •  一个人的身影
    2021-02-12 16:05

    I was looking for a way to make the trailling slash on the root of a PathPrefixRoute block optional.

    If you have, say:

    from webapp2_extras.routes import RedirectRoute, PathPrefixRoute
    
    from webapp2 import Route
    
    app = webapp2.WSGIApplication([
      PathPrefixRoute('admin', [
          RedirectRoute('/', handler='DashboardHandler', name='admin-dashboard', strict_slash=True),
          RedirectRoute('/sample-page/', handler='SamplePageHandler', name='sample-page', strict_slash=True),
      ]),
    ])
    

    You will be able to access /admin/, but not /admin.

    Since I couldn't find any better solution, I've added a redirect_to_name to an extra route, like:

    from webapp2_extras.routes import RedirectRoute, PathPrefixRoute
    
    from webapp2 import Route
    
    app = webapp2.WSGIApplication([
      Route('admin', handler='DashboardHandler', name='admin-dashboard'),
      PathPrefixRoute('admin', [
          RedirectRoute('/', redirect_to_name='admin-dashboard'),
          RedirectRoute('/sample-page/', handler='SamplePageHandler', name='sample-page', strict_slash=True),
      ]),
    ])
    

    I'm interested in better solutions to this problem.

    Should I go for Stun's solution and simply not use RedirectRoute?

提交回复
热议问题