UTF-8 encoding of JSON output characters in Django Rest_framework

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.