问题描述
'ascii'编解码器可以将数据存储在数据存储区中, t解码位置5中的字节0xc3:序号不在范围内(128)
Traceback(最近调用最后一个):
文件/ base / data / home / apps / s〜myapp-www / events。 355951895377615944 / webapp2.py,第1511行,在__call__
中rv = self.handle_exception(request,response,e)
文件/ base / data / home / apps / s〜myapp-www / events .355951895377615944 / webapp2.py,第1505行,在__call__
rv = self.router.dispatch(request,response)
文件/ base / data / home / apps / s〜myapp-www / events.355951895377615944 / webapp2.py,第1253行,在default_dispatcher
返回route.handler_adapter(请求,响应)
文件/ base / data / home / apps / s〜myapp-www / events。 355951895377615944 / webapp2.py,第1077行,在__call__
return handler.dispatch()
文件/base/data/home/apps/s~myapp-www/events.355951895377615944/handler.py ,第127行,发送
和resp onse = super(NewBaseHandler,self).dispatch()
文件/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2.py,第547行,发送
返回self.handle_exception(e,self.app.debug)
文件/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2.py,第545行,发送
返回方法(* args,** kwargs)
文件/base/data/home/apps/s~myapp-www/events.355951895377615944/handler.py,第73行,在check_login $ b中$ b返回处理程序(self,* args,** kwargs)
文件/base/data/home/apps/s~myapp-www/events.355951895377615944/handler.py,line 526,in get
user = user)
文件/base/data/home/apps/s~myapp-www/events.355951895377615944/handler.py,第91行,在render_jinja
** template_args中) )
文件/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2_extras/jinja2.py,第158行,在render_template
中返回self.environment.get_template(_filename ).render(**上下文)
文件/ base / data / h ome / apps / s〜myapp-www / events.355951895377615944 / jinja2 / environment.py,第894行,在render
中返回self.environment.handle_exception(exc_info,True)
文件template_files / my_organization .html,第148行,顶层模板代码
< li id ={{person.key.id()| makeid}}class =level_1 inactive leaf>< a href =style =class =>< ins>& nbsp;< / ins>< table class =leaf_info>< tbody> < tr>< td class =name> {{person.firstname}} {{person.lastname}} {{person.key.id()| makeid}}< / td>< td class =leveltitle =新分销商>< span class =level_parseable> 1< / span> 1< / td>< td class =downlines> 0< / td>< td class =cc_personal>< span class =cc_personal_parseable>< / span> 0< / td>< td class =cc_downlines>< span class =cc_downlines_parseable>< span> 0< / td>< td class =cc_activity>< span class =cc_activity_parseable>< / span> 0< / td>< td class =cc_nonmanager>< span class =cc_nonmanager_parseable>< / span> 0< / td>< td class =cc_total>< span class =cc_total_parseable>< / span> 0< / td>< tr>< / tbody>< / table>< / a>< / li> {%endfor%}
UnicodeDecodeError:'ascii'codec无法解码位置5中的字节0xc3:ordinal not in范围(128)
过去工作的循环是ary:
{%person for person in people%}
< a class =leaf_info>< tbody>< < tr>< td class =name> {{person.firstname}} {{person.lastname}} {{person.key.id()| makeid}}< / td>< td class =leveltitle =新分销商>< span class =level_parseable> 1< / span> 1< / td>< td class =downlines> 0< / td>< td class =cc_personal>< span class =cc_personal_parseable>< / span> 0< / td>< td class =cc_downlines>< span class =cc_downlines_parseable>< span> 0< / td>< td class =cc_activity>< span class =cc_activity_parseable>< / span> 0< / td>< td class =cc_nonmanager>< span class =cc_nonmanager_parseable>< / span> 0< / td>< td class =cc_total>< span class =cc_total_parseable>< / span> 0< / td>< TR>< / tbody的>< /表>< / A>< /锂>
{%endfor%}
我能做些什么来解决这个错误?
我的处理程序看起来像这样
class Myorg(NewBaseHandler):
@user_required
def get(self):
user = auth_models.User.get_by_id(long(self.auth.get_user_by_session()['user_id']))
people = auth_models.User .query(auth_models.User.sponsor == user.key).fetch()
self.render_jinja('my_organization.html',people = people,
user = user)
我的模型定义是来自webapp2的用户模型。这里还有我的自定义文件管理器makeid:
def makeid(n,countrycode =46):
countrycode = str(countrycode)
n = str(n)
返回%s%s%s%(countrycode,'0'*(12-len(countrycode)-len(n)),n)
更新
解决方法很奇怪,我只是做一个 .decode('utf-8')
这是不应该做的:
<$
$ b @user_required
def get(self):
user = \
auth_models。 User.get_by_id(long(self.auth.get_user_by_session()['user_id'
)))
sponsor = None
如果user.sponsor:
sponsor = user.sponsor。 get()
address = None
if user.address:
address = user.address.decode('utf-8')
if user.city:
city = user.city.decode('utf-8')
self。 render_jinja('details.html',city = city,user = user,address = address,sponsor = sponsor,form = UpdateForm(obj = user))
有没有办法一次解码用户对象的所有变量,而不是一个个地解码?
您试图将原始(字节)字符串内插到Unicode模板中。这是通过尝试使用某种编码将原始字符串解码为unicode来完成的 - 在这里,默认的ascii编码 - 由于遇到了对ASCII无效的代码点而失败。
为了解决这个问题,您只需要将unicode字符串传递到您的模板中 - 在传入之前使用正确的编码解码器解码字符串。
I get a strange error message when trying to read non-ascii from the datastore:
'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128)
Traceback (most recent call last):
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/handler.py", line 127, in dispatch
response = super(NewBaseHandler, self).dispatch()
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/handler.py", line 73, in check_login
return handler(self, *args, **kwargs)
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/handler.py", line 526, in get
user=user)
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/handler.py", line 91, in render_jinja
**template_args))
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2_extras/jinja2.py", line 158, in render_template
return self.environment.get_template(_filename).render(**context)
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "template_files/my_organization.html", line 148, in top-level template code
<li id="{{ person.key.id()|makeid }}" class="level_1 inactive leaf"><a href="" style="" class=""><ins> </ins><table class="leaf_info"><tbody> <tr><td class="name">{{ person.firstname }} {{ person.lastname}} {{person.key.id()|makeid}}</td><td class="level" title="New Distributor"><span class="level_parseable">1</span>1</td><td class="downlines">0</td><td class="cc_personal"><span class="cc_personal_parseable"></span>0</td><td class="cc_downlines"><span class="cc_downlines_parseable"></span>0</td><td class="cc_activity"><span class="cc_activity_parseable"></span>0</td><td class="cc_nonmanager"><span class="cc_nonmanager_parseable"></span>0</td><td class="cc_total"><span class="cc_total_parseable"></span>0</td></tr></tbody></table></a></li>{% endfor %}
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128)
The loop that used to work is ordinary:
{% for person in people %}
<li id="{{ person.key.id()|makeid }}" class="level_1 inactive leaf">
<a href="" style="" class=""><ins> </ins><table class="leaf_info"><tbody> <tr><td class="name">{{ person.firstname }} {{ person.lastname}} {{person.key.id()|makeid}}</td><td class="level" title="New Distributor"><span class="level_parseable">1</span>1</td><td class="downlines">0</td><td class="cc_personal"><span class="cc_personal_parseable"></span>0</td><td class="cc_downlines"><span class="cc_downlines_parseable"></span>0</td><td class="cc_activity"><span class="cc_activity_parseable"></span>0</td><td class="cc_nonmanager"><span class="cc_nonmanager_parseable"></span>0</td><td class="cc_total"><span class="cc_total_parseable"></span>0</td></tr></tbody></table></a></li>
{% endfor %}
What can I do to resolve this error?
My handler looks like this
class Myorg(NewBaseHandler):
@user_required
def get(self):
user = auth_models.User.get_by_id(long(self.auth.get_user_by_session()['user_id']))
people = auth_models.User.query(auth_models.User.sponsor == user.key).fetch()
self.render_jinja('my_organization.html', people=people,
user=user)
And my model definition is the User model from webapp2. Here is also my custom filer makeid:
def makeid(n, countrycode="46"):
countrycode = str(countrycode)
n = str(n)
return "%s%s%s" % (countrycode, '0'*(12-len(countrycode)-len(n)), n)
Update
The workaround is strange, I just make a .decode('utf-8')
which is shouldn't need to be doing:
class UpdateHandler(NewBaseHandler):
@user_required
def get(self):
user = \
auth_models.User.get_by_id(long(self.auth.get_user_by_session()['user_id'
]))
sponsor = None
if user.sponsor:
sponsor = user.sponsor.get()
address = None
if user.address:
address = user.address.decode('utf-8')
if user.city:
city = user.city.decode('utf-8')
self.render_jinja('details.html', city=city, user=user, address=address, sponsor=sponsor, form=UpdateForm(obj=user))
Is there any way to decode the all of the variables of the user object at once instead of one by one?
You're attempting to interpolate a raw (byte) string into a Unicode template. This is done by attempting to decode the raw string into unicode using some encoding - here, the default 'ascii' encoding - which is failing because it's encountering a codepoint that isn't valid for ASCII.
To fix this, you need to pass only unicode strings into your template - decode the string using the correct codec before passing it in.
这篇关于如何解决UnicodeDecodeError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!