在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字符并对其进行手动编码?

最佳答案

我找到了答案。

在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
}

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

关于django - 升级到django-rest-framework 3后的UnicodeDecodeError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35075093/

10-09 18:55