【发布时间】:2018-03-29 20:48:45
【问题描述】:
编辑了我的问题,因为之前不是很清楚。
代码 1 工作
class AtSeaHandler(webapp2.RequestHandler):
def get(self, id=None):
boat = ndb.Key(urlsafe=id).get()
boat_dict = boat.to_dict()
boat_dict['self'] = "/boats/" + id
self.response.write(json.dumps(boat_dict)
app = webapp2.WSGIApplication([
('/boats/([\w-]+)', AtSeaHandler)
], debug=True)
代码 2(不起作用)
class AtSeaHandler(webapp2.RequestHandler):
def get(self, id=None):
boat = ndb.Key(urlsafe=id).get()
boat_dict = boat.to_dict()
boat_dict['self'] = "/boats/" + id
self.response.write(json.dumps(boat_dict)
app = webapp2.WSGIApplication(
('/boats/([\w-]+)/at_sea', AtSeaHandler)
], debug=True)
代码 2 是代码 1 的副本。代码 1 和代码 2 之间的唯一区别是代码 1 是 ('/boats/([\w-]+)', AtSeaHandler) 与代码 2 是 ('/boats/ ([\w-]+)/at_sea', AtSeaHandler)。然后我注释掉代码1。
代码 1 有效。代码 2 不适用于添加的 /at_sea。我在邮递员中输入 http://localhost:8080/boats/aghkZXZ-Tm9uZXIRCxIEQm9hdBiAgICAgPCLCww 并验证代码 1 是否有效。还测试了代码 2 并验证它不起作用。
我需要做什么才能让它工作?当我将 boat 实体键传递给 ([\w-]+) 并且我 def get(self, id=None): id 被赋予船实体键时。 at_sea 是否需要 get() 中的参数?我是否需要将 get() 从 get(self, id=none) 更改为 get(self, id=none, argument3=none)?来自 C++,我一直认为这就像一个带有 3 个参数的函数调用需要一个带有 3 个参数的函数头来保存这些参数。我完全误解了事情吗?
documentation 无法帮助我理解如何回答我的问题...
【问题讨论】:
-
是的,我的第一个怀疑是正确的:尝试localhost:8080/boats/aghkZXZ-Tm9uZXIRCxIEQm9hdBiAgICAgPCLCww/… 匹配新模式
标签: regex python-2.7 google-app-engine webapp2