Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python webapp2:路由中的正则表达式_Python_Regex_Webapp2 - Fatal编程技术网

Python webapp2:路由中的正则表达式

Python webapp2:路由中的正则表达式,python,regex,webapp2,Python,Regex,Webapp2,我有这样一个问题:我需要类的一个方法来处理不同的URI(对于URI“/solution/add”和“solution/edit”)。所以我写了这样的路由: app = webapp2.WSGIApplication([webapp2.Route(r'/solutions/(add|edit)', handler='solution.SolutionPage:add_edit_solution'), ], debug=True) webapp2给出了404错误。你能提出这个问题的解决办法吗 当然

我有这样一个问题:我需要类的一个方法来处理不同的URI(对于URI“/solution/add”和“solution/edit”)。所以我写了这样的路由:

app = webapp2.WSGIApplication([webapp2.Route(r'/solutions/(add|edit)', handler='solution.SolutionPage:add_edit_solution'), ], debug=True)
webapp2给出了404错误。你能提出这个问题的解决办法吗

当然,我可以为每个URI编写不同的路由,但这并不有趣。)

如图所示,必须将正则表达式放在尖括号中,用冒号分隔名称和表达式。名称是可选的,但是

外部的所有内容都不会解释为要匹配的正则表达式

比如:
'/blog/'
,或者在您的情况下:

webapp2.Route(r'/solutions/<:(add|edit)>',
              handler='solution.SolutionPage:add_edit_solution')
webapp2.Route(r'/solutions/',
handler='solution.SolutionPage:添加\编辑\解决方案')
如果我可以添加一些内容。 出于我自己的目的,我尝试创建一个执行类似操作的处理程序,但重点是我使用了self.request.host或self.request.route而不是参数

这样做,并使用switch case或if/elif/else循环解析结果,允许我创建一个名为URIHandler的类,该类用于将任何类型的请求动态路由到正确的Resources(甚至404/500/yyy错误页),而无需为每个新的Resources重写或添加路由


因此,我有兴趣比较这两种方法,使它们适合一点。

它给出了一个错误:TypeError:add\u edit\u solution()正好接受一个参数(给定2个)。我认为发生此错误是因为此表达式用于获取参数(如“product_id”等)。@Dimitry,正确。有什么问题吗?当您将正则表达式传递给
Route
时,它会将正则表达式中的组作为位置参数或关键字参数(如果您为它们命名)调用处理程序。“,用于为路由生成URL。设置时,匹配正则表达式的值将作为关键字参数传递给处理程序。否则,它将作为位置参数传递。”因此,如果要在此处使用正则表达式,必须传递接受*args或**kwargs的处理程序。好的,是的)这很有效。非常感谢。在开始的时候,我想用不同的方式来实现它,我想在没有任何参数的情况下实现它,但正如我现在看到的,使用参数是更好的方式。