开发者

How to create the subpaths that exactly match to the users’ name

开发者 https://www.devze.com 2023-02-11 23:01 出处:网络
After I direct the users to their pages, like /example/john, how do I make the webapp.RequestHandler to handle the this page? If I do this (\'/user.*\', UsersSubPath) that matches the all pages after

After I direct the users to their pages, like /example/john, how do I make the webapp.RequestHandler to handle the this page? If I do this ('/user.*', UsersSubPath) that matches the all pages after /user/, If I do ('/user/user.name*', UsersSubPath), that’s not working either since it can’t substitute the user.name by the user’s name automatically. Bt开发者_StackOverfloww, self.redirect('something'), returns an URL string or nothing?

Thank you.


You need to capture that part of the URL using a regular expression, and then pass that captured text to your handler method as an argument, something like this:

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util


class ExampleHandler(webapp.RequestHandler):
    def get(self, name="default"):
        self.response.out.write('Hello %s!' % name)


def main():
    application = webapp.WSGIApplication([('/example/(\w+)', MainHandler)],
                                         debug=True)
    util.run_wsgi_app(application)

...in the list of handlers, the bit (\w+) tells the system to match one or more 'word' characters, and capture them together into a group. That group of characters will be passed into the get() method of the ExampleHandler class.

0

精彩评论

暂无评论...
验证码 换一张
取 消