首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从python龙卷风库中的字符串呈现html响应

从python龙卷风库中的字符串呈现html响应
EN

Stack Overflow用户
提问于 2022-01-17 15:24:36
回答 1查看 315关注 0票数 0

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

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

我该怎么做呢?龙卷风版本为6.1。现在它是如何显示的:在这里输入图像描述

与龙卷风无关的答案也值得赞赏:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-17 17:52:32

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

代码语言:javascript
复制
def get(self):
    self.write('<h1>Hello</h1>')

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

代码语言:javascript
复制
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:

代码语言:javascript
复制
def get(self):
    self.set_header('Content-Type', 'text/html')
    self.write('<h1>Hello</h1>)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70743738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档