Is there any way to pass parameters to a RequestHandler object when I create my WSGIApplication instance?
I mean
app = webapp2.WSGIApplication([
('/', MainHandler),
('/route1', Handler1),
('/route2', Handler2)
], debug=True)
Is it possible to pass some arguments to MainHandler
, Handler1
or Handler2
?
Thanks in advance
You can also pass parameters through a configuration dictionary.
First you define a configuration:
import webapp2
config = {'foo': 'bar'}
app = webapp2.WSGIApplication(routes=[
(r'/', 'handlers.MyHandler'),
], config=config)
Then access it as you need. Inside a RequestHandler, for example:
import webapp2
class MyHandler(webapp2.RequestHandler):
def get(self):
foo = self.app.config.get('foo')
self.response.write('foo value is %s' % foo)
From here: webapp2 documentation
You pass "arguments" in the URL essentially.
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'),
])`
From here: features
The page at above link no longer exists. Equivalent doc can be found here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With