gpt4 book ai didi

django - 升级到 django-rest-framework 3 后出现 UnicodeDecodeError

转载 作者:行者123 更新时间:2023-12-02 05:03:23 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com