【问题标题】:Show 404 when specific route matches特定路由匹配时显示 404
【发布时间】:2013-08-31 07:25:31
【问题描述】:

我有以下路线设置,匹配 /rest 上的休息 api。

routes = [
    PathPrefixRoute('/rest', [
        Route('/customers', handler='app.handlers.CustomerHandler:list',
                            methods=['GET']),
        # some other /rest routers
        Route('/<:.*>', ?) # this route should execute abort(404)
    ]),
    Route('/<page:.*>', handler='app.handlers.PageHandler', methods=['GET'])
]

如果有人访问/rest/does_not_exists url,我想显示 404。

有没有办法从路由调用 abort(404) 或者我应该为此创建一个处理程序?

【问题讨论】:

    标签: python google-app-engine python-2.7 webapp2


    【解决方案1】:

    因为你有一个像页面一样的全部路线,如果这与上一个问题的上下文相同,你可以这样做

    if not os.path.exists(os.path.join(template_path, page)): 
        self.abort(404)
    

    但是,您需要创建处理程序以在特定路由上调用 self.abort,尽管默认情况下未捕获的路由正是这样做的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-24
      • 2011-10-25
      • 2022-10-24
      • 2013-06-04
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      • 2018-06-28
      相关资源
      最近更新 更多