JsonResponse返回中文乱码问题

class Publish(APIView):
    def get(self, request):
        publish_list = models.Publish.objects.all()
        bs = MySer.PublishSerializer(publish_list, many=True)
        return JsonResponse(bs.data, safe=False, )
        # 当返回的中文是乱码时,这时由于ascii码的原因,JsonResponse()在初始化的时候使用了json.dumps()把字典转换成了json格式
        # 当ensure_ascii是false的时候,可以返回ASCII码的值,否则就会被JSON转义
        # 所以含有中文的字典转json字符串时,使用json.dumps()方法要把ensure_ascii参数修改成false
        # content_type是指定MIME类型和编码,这样客户端知道主体是什么类型的资源,才能调用相应的插件或内置的程序去处理
        # return HttpResponse(json.dumps(bs.data,ensure_ascii=False),content_type='application/json')
View Code

猜你喜欢

转载自www.cnblogs.com/xufengnian/p/10161597.html
今日推荐