如何配置app.yaml以支持/ user/<user-id>等网址?

LA_*_*LA_ 5 python google-app-engine

我做了以下事情

- url: /user/.*
  script: script.py
Run Code Online (Sandbox Code Playgroud)

以及script.py中的以下处理:

class GetUser(webapp.RequestHandler):
    def get(self):
        logging.info('(GET) Webpage is opened in the browser')
        self.response.out.write('here I should display user-id value')

application = webapp.WSGIApplication(
                                     [('/', GetUser)],
                                     debug=True)
Run Code Online (Sandbox Code Playgroud)

看起来有些不对劲.

ber*_*nie 5

app.yaml你想要做的事情:

- url: /user/\d+
  script: script.py
Run Code Online (Sandbox Code Playgroud)

然后在script.py:

class GetUser(webapp.RequestHandler):
    def get(self, user_id):
        logging.info('(GET) Webpage is opened in the browser')
        self.response.out.write(user_id)
        # and maybe you would later do something like this:
        #user_id = int(user_id)
        #user = User.get_by_id(user_id)

url_map = [('/user/(\d+)', GetUser),]
application = webapp.WSGIApplication(url_map, debug=True) # False after testing

def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

  • @Justin:匹配完全由正则表达式组成,每个`()`组作为位置参数传递给相关的`RequestHandler`方法. (2认同)
  • 请注意,更常见的方法是让`app.yaml`将所有请求(例如,`.*`)路由到您的应用程序 - 每页不需要单独的应用程序,或者在`app.yaml`中使用特定的RE一点都不 (2认同)