How do I return JSON without using a template in Django?

前端 未结 9 1608
再見小時候
再見小時候 2020-12-04 07:01

This is related to this question: Django return json and html depending on client python


I have a command line Python API for a Django app. When I access the a

9条回答
  •  被撕碎了的回忆
    2020-12-04 07:41

    In Django 1.7 this is even easier with the built-in JsonResponse.

    https://docs.djangoproject.com/en/dev/ref/request-response/#jsonresponse-objects

    # import it
    from django.http import JsonResponse
    
    def my_view(request):
    
        # do something with the your data
        data = {}
    
        # just return a JsonResponse
        return JsonResponse(data)
    

提交回复
热议问题