我的数据存储包含值,我希望我的表单能够更新这些值。我在jinja2中使用wtforms和谷歌应用引擎。我收到一条我无法理解的错误消息:
'ascii' codec can't decode byte 0xc3 in position 10: ordinal not in range(128)
Traceback (most recent call last):
File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/handler.py", line 152, in dispatch
response = super(NewBaseHandler, self).dispatch()
File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/handler.py", line 101, in check_login
return handler(self, *args, **kwargs)
File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/handler.py", line 881, in get
self.render_jinja('details.html', **data)
File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/handler.py", line 119, in render_jinja
**template_args))
File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/webapp2_extras/jinja2.py", line 158, in render_template
return self.environment.get_template(_filename).render(**context)
File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "template_files/details.html", line 78, in top-level template code
<dd> {{ form.address|safe }} </dd>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 10: ordinal not in range(128)
我的代码是普通的:
class UpdateForm(Form):
address = TextField(_('Address'), [validators.Required()])
zipcode = TextField(_('Zip code'), [validators.Required()])
email = TextField(_('Email'), [validators.Required()])
class UpdateHandler(NewBaseHandler):
@user_required
def get(self):
user = auth_models.User.get_by_id(long(self.auth.get_user_by_session()['user_id']))
userinfo = self.auth.get_user_by_session()
if user.address:
address = user.address#.decode('utf-8')
logging.info('user info:'+str(userinfo))
if user:
data = {'user': user, 'address': address, 'form':UpdateForm(obj=user)}
self.render_jinja('details.html', **data)
@user_required
def post(self, sponsor=None):
user = \
auth_models.User.get_by_id(long(self.auth.get_user_by_session()['user_id'
]))
user.address = UpdateForm(self.request.params).address.data
user.put()
self.render_jinja('details.html', user=user, sponsor=sponsor, form=UpdateForm(obj=user))
我想知道我能做些什么来调整问题?任何帮助是极大的赞赏。
实验上我发现这样做会处理错误:
result.result_str = unicode(my_string_variable, "utf8")
我仍然希望避免操纵我使用的所有字符串。
上次我检查问题没有发生。我怀疑这是一个问题,我的测试数据是latin-1所以我清除了我的测试数据。感谢您的帮助。
答案 0 :(得分:6)
我也使用GAE和Jinja2。我碰巧用西里尔符号来捕捉这个错误。 所以这三行代码帮助了我(我不想在代码中手动“。编码”我的所有字符串。)
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
指出了这一点
答案 1 :(得分:1)
是的,这是一个编码错误。我发现您已尝试添加.decode('utf-8')
。但是您是否尝试过编码导致错误的字符串,如.encode('utf-8')
?