7

创建 WSGIApplication 实例时,有什么方法可以将参数传递给 RequestHandler 对象?

我是说

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/route1', Handler1),
    ('/route2', Handler2)
], debug=True)

是否可以将一些参数传递给MainHandler,Handler1Handler2

提前致谢

4

2 回答 2

8

本质上,您在 URL 中传递“参数”。

class BlogArchiveHandler(webapp2.RequestHandler):
    def get(self, year=None, month=None):
        self.response.write('Hello, keyword arguments world!')

app = webapp2.WSGIApplication([
    webapp2.Route('/<year:\d{4}>/<month:\d{2}>', handler=BlogArchiveHandler, name='blog-archive'),
])`

从这里开始:功能

上面链接的页面不再存在。可以在此处找到等效文档。

于 2013-01-03T17:40:18.380 回答
8

您还可以通过配置字典传递参数。

首先定义一个配置:

import webapp2

config = {'foo': 'bar'}

app = webapp2.WSGIApplication(routes=[
    (r'/', 'handlers.MyHandler'),
], config=config)

然后根据需要访问它。在 RequestHandler 中,例如:

import webapp2

class MyHandler(webapp2.RequestHandler):
    def get(self):
        foo = self.app.config.get('foo')
        self.response.write('foo value is %s' % foo)

从这里开始:webapp2 文档

于 2013-05-10T10:25:26.280 回答