Reputation: 723
According to the this post , i want to make a rest api for my android application . but when i wan give json from the django rest framework , it's not support utf8 words. this is screenshot :
this is my model :
from __future__ import unicode_literals
from django.db import models
class News(models.Model):
title = models.CharField(max_length=255)
created = models.DateTimeField('auto_now_add = True')
active = models.BooleanField()
def __str__(self):
return self.title.encode('utf8')
this is my view :
from django.shortcuts import render
from rest_framework import viewsets
from rest_framework import permissions
from news.models import News
from news.serializers import NewsSerialzer
class NewsViewSet(viewsets.ModelViewSet):
queryset = News.objects.all()
serializer_class = NewsSerialzer
and this is my serializers:
from news.models import News
from rest_framework import serializers
class NewsSerialzer(serializers.HyperlinkedModelSerializer):
class Meta:
model = News
fields = ('title' , 'active' , 'created')
i use django rest framework 3.3.2
Upvotes: 1
Views: 1410
Reputation: 1842
Ensure that u assign correct encoding to http header so browser will recognize encoding of data. Example below show how u can identify it using Developer Tool in Firefox. Same tools exists in Chrome . If u clearly will not assign encoding in header then browser will try guest or use default to encode data from server.
Upvotes: 1
Reputation: 723
Anyway , when i see json
in the chrome +33 , response does not show utf8
word but when i see response in the Firefox every thing is Ok , and when i parse the Json
every thing is Ok .
Upvotes: 0
Reputation: 431
The problem is with self.title.encode('utf8')
. Seems like your are using python 2. The encode()
function converts string to bytes. That's why title is getting weird. Since you are already importing unicode_literals
from __future__
, all the string will be unicode. Just return self.title
that would solve the problem.. For more info https://docs.python.org/2/howto/unicode.html
Upvotes: 1