【发布时间】:2022-01-17 15:24:36
【问题描述】:
我需要像这样在龙卷风中呈现来自 html 字符串的响应:
self.method_to_render_html_from_string('<h1>Hello</h1>')
我该怎么做?龙卷风版本是 6.1。 现在它的显示方式如下: enter image description here
与龙卷风无关的答案也很受欢迎:)
【问题讨论】:
我需要像这样在龙卷风中呈现来自 html 字符串的响应:
self.method_to_render_html_from_string('<h1>Hello</h1>')
我该怎么做?龙卷风版本是 6.1。 现在它的显示方式如下: enter image description here
与龙卷风无关的答案也很受欢迎:)
【问题讨论】:
如果您想发送回复,请使用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>)
【讨论】:
self.write('<h1>Hello</h1>'),它会显示在带有<h1>标签的浏览器中。可能是什么问题?提前致谢。
text/plain 发送,但这很奇怪,因为 Tornado 默认以text/html 发送响应。但是,您尝试设置 Content-Type: text/html 标头。我已经更新了答案。