2012-07-18 34 views
5

Tôi thấy rất nhiều câu hỏi liên quan đến điều này nhưng tôi không thể tìm ra giải pháp.UnicodeEncodeError: 'ascii' codec không thể mã hóa các ký tự ở vị trí 1-4: thứ tự không nằm trong phạm vi (128)

Đây là trên Django 1.4 và Python 2.7.

data là từ điển chứa ký tự UTF8. Xem dòng này:

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

Template được kết xuất mà kết quả đầu ra giá trị từ data đó.

Tại sao nó phát nổ và tôi có thể làm gì để sửa lỗi này?

EDIT: Sau khi đào xung quanh, một phần của số đó data chứa lxml.objectify.ObjectifiedElement. Về cơ bản một phần tử XML có thể được truy vấn như một từ điển thông thường. Giá trị mà nó tạo ra dường như chuỗi unicode thích hợp như thế này: u'\xae\u2020\xa5\xa8\u02c6\xf8'

Dưới đây là toàn bộ stacktrace:

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

File "/web/mysite/current/api/views.py", dòng 163, trong hóa đơn –

Trả lời

4

Nó nên không chứa các ký tự UTF-8; nó phải chứa unicode s.

{'foo': u'bar'} 
+0

Gotcha. Tôi phát hiện ra rằng dữ liệu chứa lxml.objectify.ObjectifiedDataElement và ai biết wtf là bên trong đó. – Grocery

+0

Ok, đối tượng lxml đó có các thuộc tính có thể truy cập như một dict. Tất cả các giá trị đều có dạng unicode như sau: 'u '\ xae \ u2020 \ xa5 \ xa8 \ u02c6 \ xf8''. Vì vậy, bất kỳ đầu mối những gì có thể thổi nó lên? – Grocery

+0

@Hàng tạp hóa, bạn có thể chấp nhận câu trả lời nếu nó phù hợp với bạn. – n611x007

0

Giá trị trong lxml.objectify.ObjectifiedĐiều khoản không thực sự unicode. Bạn có thể sử dụng các wrapper sau xung quanh đối tượng ObjectifiedElement của bạn:

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 

sau đó

def some_view(request): 
    return render_to_response(
     "some_template.html", 
     { 
      "xml_data": LxmlUnicodeWrapper(your_xml_object) 
     }, 
    ) 
Các vấn đề liên quan