Creating a JSON response using Django and Python

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

    First import this:

    from django.http import HttpResponse
    

    If you have the JSON already:

    def your_method(request):
        your_json = [{'key1': value, 'key2': value}]
        return HttpResponse(your_json, 'application/json')
    

    If you get the JSON from another HTTP request:

    def your_method(request):
        response = request.get('https://www.example.com/get/json')
        return HttpResponse(response, 'application/json')
    

提交回复
热议问题