Unicode strings in tornado web app

天涯浪子 提交于 2019-12-20 04:19:59

问题


How can I use unicode strings in tornado views or templates?
I insert in template
<meta http-equiv="content-type" content="text/html;charset=utf-8" /> And in view

# -- coding: utf-8 --
Output is ????

回答1:


Once you have your unicode string ready, the request should end

self.render("template.html", aString=aUnicodeString)

This renders the file "template.html" setting the aString variable to aUnicodeString.

template.html would look something like this:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    </head>
    <body>
        {{aString}}
    </body>
</html>

It's also possible to inline the HTML in the Tornado server.

self.render('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>{{aString}}</body></html>', aString=aUnicodeString)

More on templates here:

Tornado Web Server Documentation



来源:https://stackoverflow.com/questions/5647648/unicode-strings-in-tornado-web-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!