【问题标题】:webapp2 route containing regex does not work包含正则表达式的 webapp2 路由不起作用
【发布时间】:2013-03-17 01:13:50
【问题描述】:

我想在我的一些路由中使用以下正则表达式来启用响应内容的格式:

    RedirectRoute(r'/mypath/<:|json|xml>', handler=myhandler, handler_method='mymethod', name='myhandler=mymethod', methods=['POST'], strict_slash=True),

上述 sn-p 所需的功能是:

  • POST /mypath 返回 html
  • POST /mypath/json 返回 application/json
  • POST /mypath/xml 返回 application/xml

有什么想法可以做到这一点吗?

【问题讨论】:

  • r'/mypath/&lt;:(json|xml)&gt;' 怎么样?
  • 此配置不支持第一种情况POST /mypath returns html
  • 哦。正确的。 r'/mypath(/json|/xml)*'?
  • 这不是 webapp2 路由中正则表达式的正确语法。错误的请求错误。
  • Bad request 表示 url 正确,但请求不正确。

标签: python regex google-app-engine webapp2


【解决方案1】:

路由类doesn't 在其内部变量之外支持正则表达式。所以试试

RedirectRoute('/mypath<format:|/xml|/json>', handler=myhandler, handler_method='mymethod', name='myhandler=mymethod', methods=['POST'], strict_slash=True)

您只需要在请求处理程序中从接收到的值中去掉前导斜杠。

变量的名称&lt;format: 是必需的,因为在/mypath/(带有结尾斜杠)请求的情况下,如果它为空白(<:>Bad request 错误。当 webapp 尝试创建 URL 以重定向到 /mypath as strict_slash=True 时发生错误。我想你也有同样的错误。

【讨论】:

    猜你喜欢
    • 2012-03-22
    • 2015-07-09
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多