Creating a JSON response using Django and Python

后端 未结 15 2375
温柔的废话
温柔的废话 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:40

    from django.http import HttpResponse
    import json
    
    class JsonResponse(HttpResponse):
        def __init__(self, content={}, mimetype=None, status=None,
                 content_type='application/json'):
            super(JsonResponse, self).__init__(json.dumps(content), mimetype=mimetype,
                                               status=status, content_type=content_type)
    

    And in the view:

    resp_data = {'my_key': 'my value',}
    return JsonResponse(resp_data)
    

提交回复
热议问题