【问题标题】:UnicodeDecodeError after upgrading to django-rest-framework 3升级到 django-rest-framework 3 后出现 UnicodeDecodeError
【发布时间】:2016-05-06 15:09:42
【问题描述】:

在 django-rest-framework 2 下,以下工作正常:

from rest_framework import rest_response, generics
from rest_framework.renderers import JSONRenderer, BrowsableAPIRenderer

class SomeView(generics.GenericAPIView):
    renderer_classes = JSONRenderer, BrowsableAPIRenderer

    def get(self, request, *args, **kwargs):
        ... 
        # Build a response dict with non-ascii in it
        ...
        return rest_response.Response(some_dict_with_non_ascii_in_it_somewhere)

我不必显式编码任何非 ascii...

但是,升级到 DRF 3 后,相同的代码现在会引发以下错误:

Traceback (most recent call last):
  File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/whitenoise/base.py", line 119, in __call__
    return self.application(environ, start_response)
  File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 189, in __call__
    response = self.get_response(request)
  File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/core/handlers/base.py", line 218, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/core/handlers/base.py", line 261, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django_extensions/management/technical_response.py", line 5, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/core/handlers/base.py", line 164, in get_response
    response = response.render()
  File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/template/response.py", line 158, in render
    self.content = self.rendered_content
  File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/rest_framework/response.py", line 71, in rendered_content
    ret = renderer.render(self.data, media_type, context)
  File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/rest_framework/renderers.py", line 104, in render
    separators=separators
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 250, in dumps
    sort_keys=sort_keys, **kw).encode(obj)
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 210, in encode
    return ''.join(chunks)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 671: ordinal not in range(128)

我猜 DRF 3 现在在某处有一些新的配置值,这是 DRF 2 下的默认值。我尝试将 REST_FRAMEWORK UNICODE_JSON 设置为 True,但我仍然遇到相同的错误。 .

有没有一种设置可以让这件作品表现得像 DRF 2 一样?还是 DRF 3 需要我在字典中查找非 ascii 字符并手动对其进行编码?

【问题讨论】:

    标签: django django-rest-framework


    【解决方案1】:

    我找到了答案。

    在 DRF 2 中,rest_framework.JSONRenderer.ensure_ascii 设置为 True。在 DRF 3 中,rest_framework.JSONRenderer.ensure_ascii 设置为 not api_settings.UNICODE_JSON(我之前在写问题时错过了 not ......)。

    所以为了让它表现得像 DRF 2,我需要将 'UNICODE_JSON' 设置为 False 而不是 True,就像我之前尝试过的那样(默认为 True):

    REST_FRAMEWORK = {
        ...
        'UNICODE_JSON': False
    }
    

    当然,我也可以对我的字典值进行编码,这在许多情况下可能是更好的选择。

    【讨论】:

      【解决方案2】:

      默认情况下,Python 2.7 认为字符串是二进制的。 尝试在文件顶部添加:

      from __future__ import unicode_literals
      

      这将使您的字符串默认为 unicode,并有助于正确转换它们。

      【讨论】:

      • 谢谢。我试了一下,虽然它确实做到了,所以现在所有相关的字符串在 dict 中都是 unicode,但处理 Response 对象并将 dict 转换为字符串的库代码没有那个 import 语句,所以东西在执行 ''.join()... 之前被转换回 unicode 和常规字符串的混合体
      猜你喜欢
      • 2013-11-21
      • 2014-10-06
      • 1970-01-01
      • 2017-03-23
      • 2017-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多