5

我看到大量与此相关的问题,但我无法找出解决方案。

这是在 Django 1.4 和 Python 2.7 上。

data是一个包含 UTF8 字符的字典。看到这一行:

render_to_response('application/app.html', data, context_instance=RequestContext(request))

模板被渲染,从那个输出值data

为什么它会爆炸,我该怎么做才能解决这个问题?

编辑:四处挖掘后,其中一部分data包含lxml.objectify.ObjectifiedElement. 基本上是一个可以像普通字典一样查询的 XML 元素。它产生的值似乎是正确的 unicode 字符串,如下所示:u'\xae\u2020\xa5\xa8\u02c6\xf8'

这是完整的堆栈跟踪:

File "/web/mysite/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
  response = callback(request, *callback_args, **callback_kwargs)

File "/web/mysite/current/api/views.py", line 163, in invoice
  return render_to_response('application/app.html', data, context_instance=RequestContext(request))

File "/web/mysite/env/lib/python2.7/site-packages/django/shortcuts/__init__.py", line 20, in render_to_response
  return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/loader.py", line 176, in render_to_string
  return t.render(context_instance)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 140, in render
  return self._render(context)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
  return self.nodelist.render(context)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 823, in render
  bit = self.render_node(node, context)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
  return node.render(context)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/loader_tags.py", line 123, in render
  return compiled_parent._render(context)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
  return self.nodelist.render(context)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 823, in render
  bit = self.render_node(node, context)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
  return node.render(context)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/loader_tags.py", line 62, in render
  result = block.nodelist.render(context)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 823, in render
  bit = self.render_node(node, context)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
  return node.render(context)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/defaulttags.py", line 281, in render
  return nodelist.render(context)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 823, in render
  bit = self.render_node(node, context)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
  return node.render(context)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 880, in render
  return _render_value_in_context(output, context)

File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 858, in _render_value_in_context
  value = force_unicode(value)

File "/web/mysite/env/lib/python2.7/site-packages/django/utils/encoding.py", line 74, in force_unicode
  s = unicode(str(s), encoding, errors)

UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-4: ordinal not in range(128)
4

2 回答 2

4

它不应包含UTF-8 字符;它应该包含unicodes。

{'foo': u'bar'}
于 2012-07-18T19:59:54.290 回答
0

lxml.objectify.ObjectifiedElement 中的值并不是真正的 unicode。您可以在 ObjectifiedElement 对象周围使用以下包装器:

from lxml.objectify import ObjectifiedElement, StringElement

class LxmlUnicodeWrapper(object):
    """Avoids UnicodeEncodeError when using ObjectifiedElement in templates."""
    def __init__(self, xml):
        self.xml = xml

    def __getattribute__(self, name):
        item = getattr(object.__getattribute__(self, "xml"), name)
        if type(item) == ObjectifiedElement:
            return LxmlUnicodeWrapper(item)
        elif type(item) == StringElement:
            return unicode(item)
        else:
            return item

然后

def some_view(request):
    return render_to_response(
        "some_template.html",
        {
            "xml_data": LxmlUnicodeWrapper(your_xml_object)
        },
    )
于 2015-10-02T15:55:41.627 回答