我试图建立一个能够查看以前编辑的维基。每个 wiki 条目都有自己的相对 url 路径,并且修订号会在 url 后附加“/rev=”。
这是我的代码的一部分。
class WikiPage(MainHandler):
def get(self, url, num):
num = int(num) if num else 0
... # Display the wiki-entry with that revision number
PAGE = r'(/(?:[a-zA-Z0-9_-]+/?)*)'
app = webapp2.WSGIApplication([
(r'/wiki' + PAGE + r'(?:/rev=(\d+))?', WikiPage),
... # Other Handlers
], debug = True)
这可能不是最好的方法(例如,我可以使用查询来获取修订号,但我想知道如何以这种方式做到这一点)
当我运行上面的代码时,我收到一个错误,指示 get 函数需要 3 个参数,但只提供了 2 个。 url 参数已正确传递,但修订号未传递。我还尝试将原始字符串中的组命名为 num 但它仍然不起作用。
对于如何将 2 个或更多参数传递给我的处理程序,我将不胜感激。
编辑:根据 Jamie Gómez 的回答,我尝试修改我的程序,但仍然无法使其工作(我收到 404 错误)。我也尝试过使用未命名的参数,但无济于事。我是否没有正确实现它?
class WikiPage(MainHandler):
def get(self, **kw):
url = kw.get('url')
num = kw.get('num')
num = int(num) if num else 0
... # Display the wiki-entry with that revision number
PAGE = r'<url:/(?:[a-zA-Z0-9_-]+/?)*>'
app = webapp2.WSGIApplication([
(r'/wiki' + PAGE + r'(?:/rev=<num:\d+>)?', WikiPage),
... # Other Handlers
], debug = True)
最佳答案
格式看起来错误,请查看文档的这一部分:
The URL template
┌──────────────┬────────────────────────────────────┐
│ Format │ Example │
├──────────────┼────────────────────────────────────┤
│ <name> │ '/blog/<year>/<month>' │
├──────────────┼────────────────────────────────────┤
│ <:regex> │ '/blog/<:\d{4}>/<:\d{2}>' │
├──────────────┼────────────────────────────────────┤
│ <name:regex> │ '/blog/<year:\d{4}>/<month:\d{2}>' │
└──────────────┴────────────────────────────────────┘
关于python - 如何将多个参数传递给 Google App Engine python 中的处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29957910/