Ali Bahraminezhad
Ali Bahraminezhad

Reputation: 326

How to handle both GET and POST requests in TornadoWeb framework?

I'm really new to Python community, it's been a while that I'm trying to learn Flask and Tornado frameworks.

As you know we can handle GET and POST requests together in Flask very easily, For example a simple URL routing in Flask is something like this:

@app.route('/index', methods=['GET', 'POST'])
def index():
    pass

I googled and read Tornado documentation but I couldn't find a way to handle both GET and POST requests together in Tornado.

All I found is like code below:

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('intro.html')

    def post(self):
        self.render('intro.html')

Any idea how to do it in Tornado?

Upvotes: 4

Views: 1948

Answers (3)

Ok.If you want to handle them together, try this

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.post()

    def post(self):
        self.render('intro.html')

Upvotes: 2

alecxe
alecxe

Reputation: 474221

You can go with using prepare() method:

Called at the beginning of a request before get/post/etc. Override this method to perform common initialization regardless of the request method.

class MainHandler(tornado.web.RequestHandler):
    def prepare(self):
        self.render('intro.html')

Hope that helps.

Upvotes: 9

Michael
Michael

Reputation: 16142

Try to this:

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.post()

    def post(self):
        self.render('intro.html')

It should work, if it doesn't add a comment :)

Also you can read some tornado tutorial

Upvotes: 1

Related Questions