Creating a JSON response using Django and Python

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

    With Django Class-based views you can write:

    from django.views import View
    from django.http import JsonResponse
    
    class JsonView(View):
        def get(self, request):
            return JsonResponse({'some': 'data'})
    

    and with Django-Rest-Framework you can write:

    from rest_framework.views import APIView
    from rest_framework.response import Response
    
    class JsonView(APIView):
        def get(self, request):
            return Response({'some': 'data'})
    

提交回复
热议问题