本文整理汇总了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)
示例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)
示例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)
示例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