如何配置app.yaml来支持像/ user /< user-id>这样的网址? [英] How to configure app.yaml to support urls like /user/<user-id>?

查看:151
本文介绍了如何配置app.yaml来支持像/ user /< user-id>这样的网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了以下操作:

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

并在script.py中进行以下处理:

  class GetUser(webapp.RequestHandler):
def get(self):
logging.info('(GET)网页在浏览器中打开')
self.response.out.write('这里我应该显示用户ID值')

application = webapp.WSGIApplication(
[('/',GetUser)],
debug = True)

看起来有些问题。

$ b $在 app.yaml 中,你想做类似的事情:

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

  class GetUser(webapp.RequestHandler):
def get(self,user_id):
logging.info('(GET)网页在浏览器中打开')
self.response.out.write(user_id)
#也许你以后会做这样的事情:
# user_id = int(user_id)
#user = User.get_by_id(user_id)
$ b $ url_map = [('/ user /(\d +)',GetUser),]
application = webapp.WSGIApplication(url_map,debug = True)#测试

def main():
run_wsgi_app(应用程序)

if __name__ ==' __main__':
main()


I do the following

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

And the following handling in 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)

Looks like something is wrong there.

解决方案

In app.yaml you want to do something like:

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

And then in 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()

这篇关于如何配置app.yaml来支持像/ user /< user-id>这样的网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆