如何配置app.yaml以支持/user/<user-id>这样的url?

6 投票
1 回答
623 浏览
提问于 2025-04-16 16:17

我做了以下操作:

- url: /user/.*
  script: script.py

在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)

看起来那里有些问题。

1 个回答

6

app.yaml 文件里,你想要做一些类似这样的事情:

- url: /user/\d+
  script: script.py

然后在 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()

撰写回答