Jayaram
Jayaram

Reputation: 760

Regular expression app engine

I need to retrieve whatever string comes after the / and retrieve that string in GetPost class below. eg: /test123, /blah, etc in the below app should go to class GetPost.

How can I implement the above requirements in the code below? Is there any module I need to import?

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

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

Upvotes: 0

Views: 87

Answers (1)

bossylobster
bossylobster

Reputation: 10163

You simply need to create a capture group for the expression you want:

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

and include an extra argument to your get handler:

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

so that requests to /xyz will result in captured_thing being set to 'xyz'.

Upvotes: 3

Related Questions