从python龙卷风库中的字符串中渲染html响应



我需要在龙卷风中呈现来自html字符串的响应,如下所示:

self.method_to_render_html_from_string('<h1>Hello</h1>')

我该怎么做?龙卷风版本是6.1。以下是目前的显示方式:在此处输入图像描述

答案,与龙卷风无关,也很感激:(

如果要发送响应,请使用self.write:

def get(self):
self.write('<h1>Hello</h1>')

如果要从模板字符串生成html,请使用tornado.template.Template:

from tornado.template import Template
def get(string):
t = Template('<h1>Hello {{ name }}</h1>')
self.write(t.generate(name='John'))

更新:

如果响应以纯文本形式发送,可以尝试设置Content-Type: text/html标头以HTML:形式发送响应

def get(self):
self.set_header('Content-Type', 'text/html')
self.write('<h1>Hello</h1>)

最新更新