Python App Engine : use app.yaml to control url ha

2019-07-30 14:30发布

When I control different type of pages, I move my code to another python file. But this way has disadvantage : each time I want to change url hander, I must comback to main.py to config bottom lines about url handler. for example :

app = webapp2.WSGIApplication([('/', MainPage),
                               ('/thanks',ThanksHandler),
                               ('/unit2/signup',Signup),
                               ('/unit2/successful', LoginSuccess)], debug=True)

I try to config handler in app.yaml to prevent dis advantage.

I add file blog.py in same directory and in this file, I have Blog class. And here is my blog.py file:

class Blog(BaseHandler):
    def get(self):
        self.response.out.write("Hello")

app = webapp2.WSGIApplication([('/blog', Blog)], debug=True)

Here is original file:

> handlers:
> - url: /favicon\.ico   static_files: favicon.ico   upload: favicon\.ico

- url: /.*   script: main.app

and this new file app.yaml:

handlers:
- url: /favicon\.ico   static_files: favicon.ico   upload: favicon\.ico

- url: /blog/.*   script: blog.app

- url: /.*   script: main.app

But when I goto: localhost:port/blog : 404: resource not found.

Please help me.

Thanks :)

1条回答
小情绪 Triste *
2楼-- · 2019-07-30 15:17

The /blog/.* url specification from the yaml file does not match the url specification from the blog.py file (/blog). In particular the fact that /blog/.* requires the url to have a slash after blog. If for example you use just /blog in both places it will work. Or you can use /blog/.* in both places.

The url specifiers are matched in the order in which they appear in the yaml file therefore in this particular case /blog/.* will not match on /blog but will match on the last (catch all really) /.* specifier and therefore main.py handler will be loaded and fail to match (no pattern in the call WSGIApplication constructor inside main.py).

Hope this helps. -Silviu

查看更多
登录 后发表回答