Creating a JSON response using Django and Python

后端 未结 15 2155
温柔的废话
温柔的废话 2020-11-22 06:02

I\'m trying to convert a server side Ajax response script into a Django HttpResponse, but apparently it\'s not working.

This is the server-side script:



        
15条回答
  •  一向
    一向 (楼主)
    2020-11-22 06:45

    You'll want to use the django serializer to help with unicode stuff:

    from django.core import serializers
    
    json_serializer = serializers.get_serializer("json")()
        response =  json_serializer.serialize(list, ensure_ascii=False, indent=2, use_natural_keys=True)
        return HttpResponse(response, mimetype="application/json")
    

提交回复
热议问题