ZengJuchen
ZengJuchen

Reputation: 4811

Can Tornado return HTML directly from a string?

The common usage of tornado return html is like:

return render(req, 'msg.html')

But as my message is only one sentence, I don't want build an html file, instead I wanna return html language directly. I tired

return render(req, '<h3>Invalid username</h3>')

but failed.

Is it possible to do it in tornado, how?

Upvotes: 2

Views: 3454

Answers (3)

You could also use the RequestHandler.finish method like this

class MyHandler(tornado.web.RequestHandler):
def get(self):
    ...
    ...
    self.finish('<h3>Invalid username</h3>')

This way your request will be not only written (as with using ´´write´´ method) but also finished and sent to the client (as render automatically does)

Upvotes: 1

avi
avi

Reputation: 9636

Yes, it's possible using RequestHandler.write. Sample code:

class MyHandler(tornado.web.RequestHandler):
    def get(self):
        ...
        ...
        self.write('<h3>Invalid username</h3>')

Upvotes: 3

Ben Darnell
Ben Darnell

Reputation: 22134

You can produce a response directly using RequestHandler.write

Upvotes: 2

Related Questions