免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2396 | 回复: 3
打印 上一主题 下一主题

[已解决]求助,关于bottle 中文显示的问题。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-06-08 18:58 |只看该作者 |倒序浏览
本帖最后由 iscien 于 2013-06-09 20:59 编辑

刚学习python 3 和bottle ,在python 3.3.2,bottle 0.11.6,Win7的环境下写了一个测试程序,但在总出现乱码,请大伙帮忙解决,多谢。
  1. # -*- coding: utf-8 -*-
  2. #中文测试一下
  3. import bottle
  4. #import peewee
  5. from bottle import route, run, get, post, request

  6. @route('/')
  7. def hello():
  8.     return "Hello World! 你好世界"

  9. @get('/login')
  10. def login_form():
  11.     return """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  12.         <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
  13.         <head>
  14.           <meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
  15.         </head>
  16.         <body>
  17.           <form method="POST" action="/login">
  18.                 <input name="name" type="text" />
  19.                 <input name="password" type="password" />
  20.                 <input type="submit" />
  21.           </form>
  22.         </body>
  23.         </html>"""

  24. @post('/login')
  25. def login_submit():
  26.     name = request.forms.get('name')
  27.     password = request.forms.get('password')
  28.     #print(name,type(name),"张三")
  29.    
  30.     return """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  31.         <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
  32.         <head>
  33.           <meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
  34.         </head>
  35.         <body>
  36.           name: %s , password: %s, 其它:%s
  37.         </body>
  38.         </html>""" %(name,password,"张三")

  39. run(host='localhost', port=8080, debug=True)
复制代码
B-Todo.7z (715 Bytes, 下载次数: 1)



通过
  1. name = request.forms.get('name')
复制代码
得到的是bytes,而不是unicode str
应该使用
  1. name = request.forms.getunicode('name')
复制代码
或者直接
  1. name = request.forms.name
复制代码
详细解释见http://bottlepy.org/docs/dev/tutorial.html,关于"WTForms support"的部分。
Note

In Python 2 all keys and values are byte-strings. If you need unicode, you can call FormsDict.getunicode() or fetch values via attribute access. Both methods try to decode the string (default: utf and return an empty string if that fails. No need to catch UnicodeError:

>>> request.query['city']
'G\xc3\xb6ttingen'  # A utf8 byte string
>>> request.query.city
u'G&ouml;ttingen'        # The same string as unicode

In Python 3 all strings are unicode, but HTTP is a byte-based wire protocol. The server has to decode the byte strings somehow before they are passed to the application. To be on the safe side, WSGI suggests ISO-8859-1 (aka latin1), a reversible single-byte codec that can be re-encoded with a different encoding later. Bottle does that for FormsDict.getunicode() and attribute access, but not for the dict-access methods. These return the unchanged values as provided by the server implementation, which is probably not what you want.

>>> request.query['city']
'G&Atilde;&para;ttingen' # An utf8 string provisionally decoded as ISO-8859-1 by the server
>>> request.query.city
'G&ouml;ttingen'  # The same string correctly re-encoded as utf8 by bottle

If you need the whole dictionary with correctly decoded values (e.g. for WTForms), you can call FormsDict.decode() to get a re-encoded copy.

乱码.PNG (1.52 KB, 下载次数: 9)

乱码.PNG

论坛徽章:
0
2 [报告]
发表于 2013-06-08 22:55 |只看该作者
这个框架好像用的人不多吧。。

论坛徽章:
0
3 [报告]
发表于 2013-06-09 16:47 |只看该作者
嗯,好象是不多,
我是看中它的单文件和@的路由方式。
结果现在编码出问题了,郁闷!

论坛徽章:
0
4 [报告]
发表于 2013-06-09 21:00 |只看该作者
已解决,原来通过
name = request.forms.get('name')
复制代码得到的是bytes,而不是unicode str
应该使用
name = request.forms.getunicode('name')
复制代码或者直接
name = request.forms.name


复制代码详细解释见http://bottlepy.org/docs/dev/tutorial.html,关于"WTForms support"的部分。
Note

In Python 2 all keys and values are byte-strings. If you need unicode, you can call FormsDict.getunicode() or fetch values via attribute access. Both methods try to decode the string (default: utf and return an empty string if that fails. No need to catch UnicodeError:

>>> request.query['city']
'G\xc3\xb6ttingen'  # A utf8 byte string
>>> request.query.city
u'G&ouml;ttingen'        # The same string as unicode

In Python 3 all strings are unicode, but HTTP is a byte-based wire protocol. The server has to decode the byte strings somehow before they are passed to the application. To be on the safe side, WSGI suggests ISO-8859-1 (aka latin1), a reversible single-byte codec that can be re-encoded with a different encoding later. Bottle does that for FormsDict.getunicode() and attribute access, but not for the dict-access methods. These return the unchanged values as provided by the server implementation, which is probably not what you want.

>>> request.query['city']
'G&Atilde;&para;ttingen' # An utf8 string provisionally decoded as ISO-8859-1 by the server
>>> request.query.city
'G&ouml;ttingen'  # The same string correctly re-encoded as utf8 by bottle

If you need the whole dictionary with correctly decoded values (e.g. for WTForms), you can call FormsDict.decode() to get a re-encoded copy.

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP