How do I make a trailing slash optional with webapp2?

后端 未结 9 818
感情败类
感情败类 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:28

    I don't like the RedirectRoute class because it causes an unnecessary HTTP Redirect.
    Based on the documentation for webapp2 Route class, here is a more detailed answer in this webapp2.Route with optional leading part thread.

    Short Answer

    My route patterns works for the following URLs.

    1. /
    2. /feed
    3. /feed/
    4. /feed/create
    5. /feed/create/
    6. /feed/edit/{entity_id}
    SITE_URLS = [
        webapp2.Route(r'/', handler=HomePageHandler, name='route-home'),
    
        webapp2.Route(r'/feed/<:(create/?)|edit/>',
            handler=MyFeedHandler,
            name='route-entity-create-or-edit'),
    
        webapp2.SimpleRoute(r'/feed/?',
            handler=MyFeedListHandler,
            name='route-entity-list'),
    ]
    

    Hope it helps :-)

提交回复
热议问题