Recently, in cooperation with Japan to develop the Jira docking release system version,
Encounter this problem.
Is our side of the output browser is displayed in Chinese, and to the Jira side is garbled.
Checked the document, the original Django Rest_framework default JSON is not specified encoding,
Needs to be displayed with the receiver's environment code.
So, because of the project progress, we were forced to encode the operation.
To view the source code for the rest framework:
Class Jsonrenderer (Baserenderer): "" " Renderer which serializes to JSON. " " Media_type = ' Application/json ' format = ' json ' encoder_class = encoders. Jsonencoder ensure_ascii = Not api_settings. Unicode_json compact = api_settings. Compact_json # We don ' t set a charset because JSON is a binary encoding, # so can be encoded as UTF-8, Utf-16 or utf-32. # see:http://www.ietf.org/rfc/rfc4627.txt # also:http://lucumr.pocoo.org/2013/7/19/ application-mimetypes-and-encodings/ charset = None
So, we rewrite a utf8jsonrenderer that inherits from Jsonrenderer. Then, you specify a Renderer_classes property value.
From rest_framework.renderers import Jsonrendererclass utf8jsonrenderer (jsonrenderer): charset = ' Utf-8 '
Class Deploypoolviewset (Viewsets. Modelviewset): "" "this viewset automatically provides ' list ', ' Create ', ' Retrieve ', ' update ' and ' Destroy ' Actions. Additionally we also provide an extra ' highlight ' action. "" Serializer_class = Deploypoolserializer authentication_classes = (tokenauthentication,) renderer_classes = ( Utf8jsonrenderer,)
UTF-8 encoding of JSON output characters in Django Rest_framework