5

I am using django-rest-framework to create an api for my application. My application uses greek letters as values of its models. I have created my viewsets and used UnicodeJSONRenderer to return the json result.

class ChapterViewSet(viewsets.ModelViewSet):
    queryset = Chapter.objects.all()
    serializer_class = ChapterSerializer
    renderer_classes = (UnicodeJSONRenderer, )

Json is returned but the greek letters are not recognized by the browser("Ξ ΟΟΟΞΈΞ΅ΟΞ·). On chrome's dev console though on network tab the preview of the response shows greek letters normally. How can I make my browser recognize greek letters?

Apostolos
  • 6,775
  • 14
  • 61
  • 132

3 Answers3

5

It's a browser issue.

UTF-8 is the default encoding for JSON content; Django Rest Framework is encoding your JSON properly into UTF-8, but your browser is not displaying it correctly.

Browsers would display it correctly if provided with a charset=utf-8 in the Content-Type HTTP Header. However, the spec defines another way of determining the encoding, so this should not be used. Django Rest Framework honours this by not including it.

There is an open ticket for Chrome on this, but unfortunately nobody seems to care. Other browsers seem to have the same problem. See also this SO question.

Community
  • 1
  • 1
e18r
  • 5,364
  • 2
  • 38
  • 38
2

This is a really weird issue that I ran into too; my first impression was that Django REST Framework was supposed to set the charset to UTF-8 in the Content-Type header, but this has already been filed as issue #2891 and there is apparently a lot of contention over it.

The fix I ended up using is simply to set the UNICODE_JSON setting to False. This results in larger responses, especially if you have lots of unicode characters in your response, as e.g. a horizontal ellipsis becomes \u2026 in a string rather than its equivalent 3-byte UTF-8 representation, but it's less likely to be misunderstood by clients.

Atul Varma
  • 163
  • 1
  • 7
0

What fixed for me (I needed accent because of pt-BR)

Go to your settings.py and include

REST_FRAMEWORK = {
    #this bit makes the magic.
    'DEFAULT_RENDERER_CLASSES': (
         #UnicodeJSONRenderer has an ensure_ascii = False attribute,
         #thus it will not escape characters.
        'rest_framework.renderers.UnicodeJSONRenderer',
         #You only need to keep this one if you're using the browsable API
        'rest_framework.renderers.BrowsableAPIRenderer',
    )
}

By doing so, you won't need to include the serializer renderer_classes on every view you have.

Hope it fixes for you too!

msaad
  • 599
  • 2
  • 11
  • But I am allready using UnicodeJsonRenderer as a renderer_class on my viewset class – Apostolos Oct 19 '14 at 17:30
  • An update on this in case someone is trying to use it: http://www.django-rest-framework.org/topics/3.0-announcement/#unicode-json-by-default – Demetris Apr 12 '17 at 12:17