How do I JSON serialize a Python dictionary?

后端 未结 5 2282
余生分开走
余生分开走 2020-12-07 11:06

I\'m trying to make a Django function for JSON serializing something and returning it in an HttpResponse object.

def json_response(something):
          


        
5条回答
  •  粉色の甜心
    2020-12-07 11:51

    With newer versions of Django you can just use JsonResponse provided by django.http:

    from django.http import JsonResponse
    
    def my_view(request):
        json_object = {'howdy': True}
        return JsonResponse(json_object)
    

    You can find more details in the official docs.

提交回复
热议问题