I have two handlers. In the first one (FooHandler) I show a form, in the GET method, and get the value of the field, POST method. Once obtained that value I wanna pass it to another handler through the URI. Then the BarHandler catch it and is able to make a query.
class FooHandler(tornado.web.RequestHandler):
def get(self):
self.render("templates/fooForm.html")
def post(self):
var1 = self.get_argument('var1') #number
self.redirect('/query/{}'.format(var1))
class BarHandler(tornado.web.RequestHandler):
def get(self, var1):
q = Query....
def main():
io_loop = tornado.ioloop.IOLoop.instance()
connect("test", host="localhost", port=27017, io_loop=io_loop)
app = tornado.web.Application(
[
(r"/", FooHandler),
(r"/query/\d+", BarHandler)
], debug = True,
)
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
main()
I get this error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 1443, in _execute
result = method(*self.path_args, **self.path_kwargs)
TypeError: get() takes exactly 2 arguments (1 given)
I'm not sure how to pass the var1 from FooHandler and catch it in BarHandler. Any suggestion?
From the documentation:
Any groups in the regex will be passed in to the handler’s get/post/etc methods as arguments.
You will need to use a group in your regex path, if you want pass a part of the path to the handler.
You should define your path as:
(r"/query/(\d+)", BarHandler)