tornado默認是轉義所有字符,比較安全,但有時候我們的確需要把字符當做html來解析處理,因此我們需要做些處理。
示例:
main.py 代碼:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render('main.html',title = '<h1>Title</h1>')
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
main.html 代碼:
{{ title }}
執行結果:

默認把h1標簽給轉義,我們可以這麼干使它不要轉義
main.html 代碼:
{% raw title %}
{{ title }}
raw 是不要轉義
執行:

或者在頭部這麼定義
main.html
{% autoescape None %}
{{ title }}
{{ escape(title) }}
{% autoescape None %} 是整個文件都生效,但可以使用escape轉義某一處
執行:
