I'm using django as the backend to a webapp. I'm sending json data via django and it has worked fine. However recently I have started dealing with non-ascii data and noticed some unusual behavior with the non-ascii characters. In my webapp I have code that looks like this:
def make_json():
json_string = u{"start_location" : "5802 W 71st St Indianapolis Indiana 46278 United States", "lat" : 39.8819269, "lng" : -86.2631006, "timezone" : "America/Indiana/Indianapolis"}
return HttpResponse(json_string, content_type='application/json')
Django doesn't have any problem with it, but when I view it in my browser (chrome), what I see is this:
{"start_location" : "5802 W 71st St Indianapolis‎ Indiana‎ 46278 United States", "lat" : 39.8819269, "lng" : -86.2631006, "timezone" : "America/Indiana/Indianapolis"}
Am I doing something wrong here? I have tried encoding the unicode object as utf-8 before giving it to HttpResponse() but it doesn't change anything.
Thanks for all the help!
I figured this out. Hopefully anyone who has the same problem can google this.
The solution is to change the content_type to:
return HttpResponse(json_string, content_type='application/json; charset=utf-8')
This is my contribution, return model serialize to json with UTF-8
dptos = Departamento.objects.all()
json_list = list(dptos.values())
return JsonResponse(json_list,safe=False,json_dumps_params={'ensure_ascii':False})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With