當前位置: 首頁>>代碼示例>>Python>>正文


Python parsers.JSONParser方法代碼示例

本文整理匯總了Python中rest_framework.parsers.JSONParser方法的典型用法代碼示例。如果您正苦於以下問題:Python parsers.JSONParser方法的具體用法?Python parsers.JSONParser怎麽用?Python parsers.JSONParser使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在rest_framework.parsers的用法示例。


在下文中一共展示了parsers.JSONParser方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: snippet_list

# 需要導入模塊: from rest_framework import parsers [as 別名]
# 或者: from rest_framework.parsers import JSONParser [as 別名]
def snippet_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        snippets = Snippet.objects.all()
        serializer = SnippetSerializer(snippets, many=True)
        return JSONResponse(serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        return JSONResponse(serializer.errors, status=400) 
開發者ID:keithadavidson,項目名稱:ansible-mezzanine,代碼行數:18,代碼來源:views.py

示例2: snippet_detail

# 需要導入模塊: from rest_framework import parsers [as 別名]
# 或者: from rest_framework.parsers import JSONParser [as 別名]
def snippet_detail(request, pk):
    """
    Retrieve, update or delete a code snippet.
    """
    try:
        snippet = Snippet.objects.get(pk=pk)
    except Snippet.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = SnippetSerializer(snippet)
        return JSONResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(snippet, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        return JSONResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        snippet.delete()
        return HttpResponse(status=204) 
開發者ID:keithadavidson,項目名稱:ansible-mezzanine,代碼行數:26,代碼來源:views.py

示例3: toy_detail

# 需要導入模塊: from rest_framework import parsers [as 別名]
# 或者: from rest_framework.parsers import JSONParser [as 別名]
def toy_detail(request, pk):
    try:
        toy = Toy.objects.get(pk=pk)
    except Toy.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    if request.method == 'GET':
        toy_serializer = ToySerializer(toy)
        return JSONResponse(toy_serializer.data)
    elif request.method == 'PUT':
        toy_data = JSONParser().parse(request)
        toy_serializer = ToySerializer(toy, data=toy_data)
        if toy_serializer.is_valid():
            toy_serializer.save()
            return JSONResponse(toy_serializer.data)
        return JSONResponse(toy_serializer.errors, \
            status=status.HTTP_400_BAD_REQUEST)
    elif request.method == 'DELETE':
        toy.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
開發者ID:PacktPublishing,項目名稱:Django-RESTful-Web-Services,代碼行數:21,代碼來源:views.py

示例4: flight_list

# 需要導入模塊: from rest_framework import parsers [as 別名]
# 或者: from rest_framework.parsers import JSONParser [as 別名]
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        print(schedules_serializer.data)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parse(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
開發者ID:PacktPublishing,項目名稱:-Full-Stack-Web-Development-with-Django-2.x-and-Angular-8,代碼行數:23,代碼來源:views.py

示例5: flight_detail

# 需要導入模塊: from rest_framework import parsers [as 別名]
# 或者: from rest_framework.parsers import JSONParser [as 別名]
def flight_detail(request, pk):
    try:
        schedule = Schedule.objects.get(pk=pk)
    except Schedule.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    
    #Retrive one record
    if request.method == 'GET':
        schedule_serializer = ScheduleSerializer(schedule)
        print(schedule_serializer.data)
        return JsonResponse(schedule_serializer.data,safe=False)

    #Update one record
    if request.method == 'PUT':
        schedule_data = JSONParser().parse(request)
        schedule_serializer = ScheduleSerializer(schedule, data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data,safe=False)  
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)

    #Delete one record
    if request.method == 'DELETE':
        schedule.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
開發者ID:PacktPublishing,項目名稱:-Full-Stack-Web-Development-with-Django-2.x-and-Angular-8,代碼行數:27,代碼來源:views.py

示例6: flight_list

# 需要導入模塊: from rest_framework import parsers [as 別名]
# 或者: from rest_framework.parsers import JSONParser [as 別名]
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
開發者ID:PacktPublishing,項目名稱:-Full-Stack-Web-Development-with-Django-2.x-and-Angular-8,代碼行數:22,代碼來源:views.py

示例7: flight_detail

# 需要導入模塊: from rest_framework import parsers [as 別名]
# 或者: from rest_framework.parsers import JSONParser [as 別名]
def flight_detail(request, pk):
    try:
        schedule = Schedule.objects.get(pk=pk)
    except Schedule.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    
    #Retrive one record
    if request.method == 'GET':
        schedule_serializer = ScheduleSerializer(schedule)
        return JsonResponse(schedule_serializer.data)

    #Update one record
    if request.method == 'PUT':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(schedule, data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)

    #Delete one record
    if request.method == 'DLETE':
        schedule.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
開發者ID:PacktPublishing,項目名稱:-Full-Stack-Web-Development-with-Django-2.x-and-Angular-8,代碼行數:26,代碼來源:views.py


注:本文中的rest_framework.parsers.JSONParser方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。