webapp2路由以匹配所有其他路径 [英] webapp2 Route to match all other paths

查看:112
本文介绍了webapp2路由以匹配所有其他路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主应用中包含以下代码。我希望除前两个路径以外的所有路径都将被最后一个路径(/.*)捕获。但我收到404错误。我缺少什么?

I've the following code in my main app. I expect all paths other than the first two to be caught by the last route (/.*). But I get 404 error. What am I missing?

  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函数中包含一个命名参数。这是唯一的方式吗?还是我错过了什么?谢谢。

I can change the last route from "/." to "/<:.>" to catch all other paths, but that also requires me to include a named parameter to MainPage.get function. Is that the only way to do or am I missing something? Thanks.

推荐答案

根据 URI模板文档,这应该可以解决问题:

According to the URI template docs, this should do the trick:

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

您可能需要定义您的 MainPage.get 方法如下,并接受额外的参数:

You may need to define your MainPage.get method as follows to accept with the extra arguments:

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

这篇关于webapp2路由以匹配所有其他路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆