Google App Engine:如何从URL处理程序向脚本传递参数?
这是我app.yaml
文件的一部分:
handlers:
- url: /remote_api
script: $PYTHON_LIB/google/appengine/ext/remote_api/handler.py
login: admin
- url: /detail/(\d)+
script: Detail.py
- url: /.*
script: Index.py
我想让这个捕获组(用(\d)
表示的那个)在脚本Detail.py
中可以使用。我该怎么做呢?
我需要想办法从Detail.py
中获取GET数据吗?
另外,当我访问像/fff
这样的URL时,它应该匹配Index.py
处理程序,但我只得到一个空白的响应。
1 个回答
12
我看到有两个问题,一个是如何把网址路径中的元素当作变量传递给处理程序,另一个是如何让通配符正常工作。
这两个问题其实和处理程序中的main()方法关系更大,而不是和app.yaml文件有关。
1) 如果你想在/detail/(\d)
这个网址中传递id,你可以这样做:
class DetailHandler(webapp.RequestHandler):
def get(self, detail_id):
# put your code here, detail_id contains the passed variable
def main():
# Note the wildcard placeholder in the url matcher
application = webapp.WSGIApplication([('/details/(.*)', DetailHandler)]
wsgiref.handlers.CGIHandler().run(application)
2) 如果你想确保你的Index.py
能捕捉到所有内容,你可以这样做:
class IndexHandler(webapp.RequestHandler):
def get(self):
# put your handler code here
def main():
# Note the wildcard without parens
application = webapp.WSGIApplication([('/.*', IndexHandler)]
wsgiref.handlers.CGIHandler().run(application)
希望这些能帮到你。