webapp2匹配所有其他路径的路由

时间:2013-09-19 11:35:02

标签: routes webapp2

我的主应用程序中有以下代码。我希望除了前两个之外的所有路径都被最后一个路由(/.*)捕获。但我得到404错误。我错过了什么?

  import webapp2
  from webapp2 import WSGIApplication, Route

  # ---- main handler
  class MainPage(webapp2.RequestHandler):
    def get(self):
      ret = jinja2render.DoRender(self)
      return ret

  routes = [
    Route (r'/rpc', handler = 'rpc.RPCHandler'),
    Route (r'/secured/somesecuredpage', handler = 'secured.securedPageHandler'),
    Route (r'/.*', handler = MainPage),
  ]

  app = WSGIApplication(routes, debug=True)

我可以将最后一条路线从“/。”更改为“/<:.>”捕获所有其他路径,但这也要求我在MainPage.get函数中包含一个命名参数。这是唯一的办法,还是我错过了什么?感谢。

1 个答案:

答案 0 :(得分:8)

根据URI template docs,这应该可以解决问题:

Route (r'/<:.*>', handler=MainPage)

您可能需要按照以下方式定义MainPage.get方法,以接受额外的参数:

def get(self, *args, **kwargs):