问题描述
当我控制不同类型的页面时,我将我的代码移动到另一个python文件。但这种方式有缺点:每次我想更改url hander时,我必须重新设置 main.py
来配置关于url处理程序的底线。例如:
app = webapp2.WSGIApplication([('/',MainPage),
('/ thanks ',ThanksHandler),
('/ unit2 / signup',注册),
('/ unit2 / successful',LoginSuccess)],debug = True)
我尝试在 app.yaml
中配置处理程序以防止出现不利情况。
我在同一个目录下添加文件 blog.py
,并且在这个文件中,我有Blog类。这里是我的 blog.py
文件:
class Blog(BaseHandler ):
def get(self):
self.response.out.write(Hello)
app = webapp2.WSGIApplication([('/ blog',Blog )],debug = True)
这是原始文件:
>处理程序:
> - url:/favicon\.ico static_files:favicon.ico上传:favicon \.ico
$ b $ - url:/.* script:main.app
和这个新文件 app.yaml
:
处理程序:
- url:/favicon\.ico static_files:favicon.ico上传:favicon\.ico
- url:/blog/.* script:blog.app
- url:/.* script:main.app
但当我转到时:localhost:port / blog:404:找不到资源。 请帮助我。
感谢:)
yaml中的/blog/.* url规范文件与blog.py文件(/ blog)中的url规范不匹配。特别是/blog/.*要求网址在博客之后有斜线。例如,如果你在两个地方都使用/博客,它就会起作用。或者你可以在两个地方使用/blog/.*。
url说明符按它们出现在yaml文件中的顺序进行匹配,因此在这种情况下/blog/.*将不匹配/ blog,但会匹配在最后(捕获所有真正的)/.*说明符,因此main.py处理程序将被加载并且无法匹配(在main.py中调用WSGIApplication构造函数中没有模式)。
希望这会有所帮助。
-Silviu
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 :)
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
这篇关于Python App Engine:使用app.yaml来控制url处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!