gpt4 book ai didi

python - 谷歌应用引擎重写问题

转载 作者:太空狗 更新时间:2023-10-30 02:34:54 25 4
gpt4 key购买 nike

如何为 python 重写 url:

http://localhost:8081/?page=1

http://localhost:8081/1

这是我的代码,但它不起作用:

class MainPage(webapp.RequestHandler):
def get(self, page):
mypage = self.request.get('page')
self.response.headers['Content-Type'] = 'text/plain'
if mypage == "":
self.response.out.write('Hello, webapp World!')
else:
self.response.out.write('page is ' + mypage)


application = webapp.WSGIApplication([('/', MainPage),('/(\d+)', MainPage)], debug=True)

最佳答案

您可以在 Controller 中使用正则表达式。这不是 Apache 风格的 URL 重写本身,但它完成了工作。重写的参数作为参数传递给处理程序。

class MyRequestHandler(webapp.RequestHandler):
def get(self, page):
self.response.headers['Content-Type'] = 'text/plain'
if not page:
self.response.out.write('Hello, webapp World!')
else:
self.response.out.write('page is ' + page)

url_map = [('/(\d+)', MyRequestHandler)]
application = webapp.WSGIApplication(url_map, debug=True)

参见 How to configure app.yaml to support urls like /user/<user-id>?用于类似的应用。

关于python - 谷歌应用引擎重写问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5779879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com