简体   繁体   English

Python App Engine:使用app.yaml来控制url处理程序

[英]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.

相关问题 Google App Engine | Python |的app.yaml - Google App Engine | Python | APP.YAML Google App Engine “解析 ./app.yaml 时出错:未知 url 处理程序类型” - Google App Engine “Error parsing ./app.yaml: Unknown url handler type” app.yaml文件:运行2个Python文件Google App Engine - app.yaml file: Running 2 Python Files Google App Engine 如何在登录名中使用自定义身份验证:app.yaml中的required属性(Google App引擎,python) - How to use custom authentication with the login: required attribute in app.yaml ( Google app engine, python ) main.py或app.yaml是否确定此示例中App Engine cron任务使用的URL? - Does main.py or app.yaml determine the URL used by the App Engine cron task in this example? 设置app.yaml URL处理程序时,如何将URL格式化为脚本? - When setting up app.yaml url handler, how to format url to script? 如何为Google App Engine应用程序写入`app.yaml`文件? - How to write `app.yaml` file for Google App Engine app? app.yaml 处理程序登录:管理选项对标准 env python GAE 应用程序无效? - app.yaml handler login: admin option not effective on standard env python GAE app? Google App Engine中的身份验证:app.yaml与python代码 - Authentication in Google App Engine: app.yaml vs. python code 如何使用Google App Engine Python将变量从app.yaml传递到main.py. - How to pass a variable from app.yaml to main.py with Google App Engine Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM