lairtech
lairtech

Reputation: 2417

Tornado: How to render response template from String?

For my request handler, my template is defined as string, not a file. I tried rendering with this, but received this error:

File "c:\envs\pomo\lib\site-packages\tornado\template.py", line 365, in _create_template f = open(path, "rb")

SESSIONS_TEMPLATE = template.Template('''<html><body>

{{sessions}}    
</body></html>
''')

class MyHandler(tornado.web.RequestHandler):
    def get(self):        
        self.render(SESSIONS_TEMPLATE.generate(sessions=response))

Upvotes: 3

Views: 2102

Answers (1)

Nykakin
Nykakin

Reputation: 8747

Use self.finish instead of self.render:

class MyHandler(tornado.web.RequestHandler):
    def get(self):        
        self.finish(SESSIONS_TEMPLATE.generate(sessions=response))

If you look at render() method you will see it uses render_string() method to generate string, inserts stuff like CSS and JS and then in the last line it uses finish() to actually create request. In your case all you have to do is that last call.

Upvotes: 4

Related Questions