简体   繁体   English

龙卷风:如何从String渲染响应模板?

[英]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") 文件“c:\\ envs \\ pomo \\ lib \\ site-packages \\ tornado \\ template.py”,第365行,在_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))

Use self.finish instead of self.render : 使用self.finish而不是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. 如果你看一下render()方法,你会看到它使用render_string()方法生成字符串,插入像CSS和JS这样的东西,然后在最后一行使用finish()来实际创建请求。 In your case all you have to do is that last call. 在你的情况下,你所要做的就是最后一次通话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM