Crossin的编程教室

标题: Python 实战 web.py html模板中有中文会乱码 [打印本页]

作者: April'scode    时间: 2019-1-20 15:29
标题: Python 实战 web.py html模板中有中文会乱码
web.py html模板中有中文会乱码(如<p>中文测试</p>页面访问的时候会出现乱码),如果是带有中文的数据传到模板的参数中,访问页面就显示正常。如$def with (moive) $for m in moive: <p>$m['title']</p>这个就能正常显示。这种情况怎么解决???

作者: April'scode    时间: 2019-1-20 15:30
上传怎么没用啊   禁掉了吗?
作者: April'scode    时间: 2019-1-20 15:43
$def with (movies)
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>April's Home Page重恩O</title>
</head>
<body>
<p>欢迎来到</p>
<h1>April's 影评</h1>
<p>影片列表</p>
$for m in movies:
    <li>
        $m['title']
    </li>
<p>jintgeg</p>
</body>
</html>
以上为index.html模板的代码,只有在 $ 参数中的  中文可以正常显示,其他的英文都正常,中文会乱码。
作者: April'scode    时间: 2019-1-20 15:49
April'scode 发表于 2019-1-20 15:43
$def with (movies)

我试了在本地打开index.html文件是正常显示的,所以我猜测是到render返回的问题,考虑是不是要在这改编码啥的,试了在Python开头加上了utf-8的声明  没反应   又试了在render周围加encode,会报错。苦恼。
作者: April'scode    时间: 2019-1-22 13:26
问题已解决,在报错的时候找到最后一行
D:\Python36\Lib\site-packages\web\template.py, line 1016:

return Template(open(path).read(), filename=path, **self._keywords)



return Template(open(path,encoding='utf-8').read(), filename=path, **self._keywords)

搞定。




欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5