0

我需要检索后面的任何字符串,并在下面的类中/检索该字符串。GetPost例如:下面的应用程序/test123/blah的等应该去上课GetPost

如何在下面的代码中实现上述要求?我需要导入任何模块吗?

class GetPost(webapp2.RequestHandler):
    def get(self):
        self.response.write("Permalink")

app = webapp2.WSGIApplication([ 
    ('/', HomePage),
    ('/new-post', NewPost),
    ('/create-post', CreatePost),
    ('/.+', GetPost)
    ], debug=True);
4

1 回答 1

3

您只需要为您想要的表达式创建一个捕获组:

app = webapp2.WSGIApplication([ 
    ...
    ('/(.+)', GetPost)
    ...

并在您的 get 处理程序中包含一个额外的参数:

class GetPost(webapp2.RequestHandler):
    def get(self, captured_thing):
        self.response.write(captured_thing)

这样请求/xyz将导致captured_thing设置为'xyz'.

于 2013-03-22T04:42:33.417 回答