Creating a JSON response using Django and Python

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

    This is my preferred version using a class based view. Simply subclass the basic View and override the get()-method.

    import json
    
    class MyJsonView(View):
    
        def get(self, *args, **kwargs):
            resp = {'my_key': 'my value',}
            return HttpResponse(json.dumps(resp), mimetype="application/json" )
    

提交回复
热议问题