求助,关于bottle +python 3 中文显示的问题。

iscien 2013-06-08 08:06:03
刚学习python 3 和bottle ,在python 3.3.2,bottle 0.11.6,Win7的环境下写了一个测试程序,但在总出现乱码,请大伙帮忙解决,多谢。




# -*- coding: utf-8 -*-
#中文测试一下
import bottle
#import peewee
from bottle import route, run, get, post, request

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

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

@post('/login')
def login_submit():
name = request.forms.get('name')
password = request.forms.get('password')
#print(name,type(name),"张三")

return """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
</head>
<body>
name: %s , password: %s, 其它:%s
</body>
</html>""" %(name,password,"张三")

run(host='localhost', port=8080, debug=True)

...全文
454 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ImN1 2013-06-09
  • 打赏
  • 举报
回复
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 另外浏览器右键看看是否浏览器把编码识别错了
iscien 2013-06-09
  • 打赏
  • 举报
回复
OK,果然是这个问题,看来还是文档看的够多、不够细啊。多谢panghuhu250 和snmr_com
ImN1 2013-06-09
  • 打赏
  • 举报
回复
果然是按bytes处理了 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: utf8) 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ö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öttingen' # An utf8 string provisionally decoded as ISO-8859-1 by the server >>> request.query.city 'Gö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.
panghuhu250 2013-06-09
  • 打赏
  • 举报
回复
或者用下面的办法:

name = request.forms.name
详细解释见http://bottlepy.org/docs/dev/tutorial.html,关于"WTForms support"的部分。
panghuhu250 2013-06-09
  • 打赏
  • 举报
回复
name = request.forms.get('name')
name得到的是str,而不是unicode str。改成:

name = request.forms.getunicode('name')
ImN1 2013-06-09
  • 打赏
  • 举报
回复
这样啊……有点不解 你的py文件应该也是utf-8的吧? 从图上面看是按单字节处理了 试一下 name = name.encode('utf-8').decode('utf-8') 再输出
iscien 2013-06-09
  • 打赏
  • 举报
回复
已经确认浏览器编码是utf-8

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧