7

尝试从数据存储区读取非 ascii 时收到一条奇怪的错误消息:

'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>&nbsp;</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)

过去工作的循环很普通:

{% for person in people %}
    <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="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 %}

我能做些什么来解决这个错误?

我的处理程序看起来像这样

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)
    return "%s%s%s" % (countrycode, '0'*(12-len(countrycode)-len(n)), n)

更新

解决方法很奇怪,我只是做了一个.decode('utf-8')不需要做的事情:

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))

有没有办法一次解码用户对象的所有变量而不是一个一个?

4

3 回答 3

9

您正在尝试将原始(字节)字符串插入 Unicode 模板。这是通过尝试使用某种编码将原始字符串解码为 un​​icode 来完成的——这里是默认的“ascii”编码——这是失败的,因为它遇到了一个对 ASCII 无效的代码点。

要解决此问题,您只需将 unicode 字符串传递到模板中 - 在传递之前使用正确的编解码器对字符串进行解码。

于 2012-01-08T23:17:21.483 回答
7

最好的方法是将字符串转换为 ASCII 字符集。您可以使用 python encode cgi 函数转换为 ASCII

s = string_1.encode("ascii", "ignore")

例子

于 2012-01-13T19:43:06.237 回答
3

将您传递给模板的文本/HTML 转换为 Unicode,您应该会看到它消失了。之前在使用 webapp2 的 GAE 中使用 Django 模板时遇到过这个问题。

于 2012-01-08T22:55:15.603 回答