[英]Python App Engine : use app.yaml to control url handler
When I control different type of pages, I move my code to another python file. 当我控制不同类型的页面时,我将我的代码移动到另一个python文件。 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. 但是这种方式有缺点:每次我想改变url hander,我必须将
main.py
到url处理程序的底线。 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. 我尝试在
app.yaml
配置处理程序以防止不利。
I add file blog.py
in same directory and in this file, I have Blog class. 我在同一目录中添加文件
blog.py
,在这个文件中,我有Blog类。 And here is my blog.py
file: 这是我的
blog.py
文件:
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
: 这个新文件
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. 但是当我转到:localhost:port / blog:404:找不到资源。
Please help me. 请帮我。
Thanks :) 谢谢 :)
The /blog/.* url specification from the yaml file does not match the url specification from the blog.py file (/blog). yaml文件中的/blog/.* url规范与blog.py文件(/ blog)中的url规范不匹配。 In particular the fact that /blog/.* requires the url to have a slash after blog.
特别是/blog/.*要求url在博客之后有斜杠这一事实。 If for example you use just /blog in both places it will work.
例如,如果您在两个地方都使用/ blog它将起作用。 Or you can use /blog/.* in both places.
或者你可以在两个地方使用/blog/.*。
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). url说明符按照它们在yaml文件中出现的顺序进行匹配,因此在这种特殊情况下/blog/.*将在/ blog上不匹配,但会匹配最后一个(全部捕获)/.*说明符,因此主要.py处理程序将被加载并且无法匹配(在main.py中调用WSGIApplication构造函数中没有模式)。
Hope this helps. 希望这可以帮助。 -Silviu
-Silviu
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.