本文整理汇总了Python中django.http.response.JsonResponse方法的典型用法代码示例。如果您正苦于以下问题:Python response.JsonResponse方法的具体用法?Python response.JsonResponse怎么用?Python response.JsonResponse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.http.response
的用法示例。
在下文中一共展示了response.JsonResponse方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: random_suggestion
# 需要导入模块: from django.http import response [as 别名]
# 或者: from django.http.response import JsonResponse [as 别名]
def random_suggestion(cls, request: WSGIRequest) -> HttpResponse:
"""This method returns a random suggestion from the database.
Depending on the value of :param playlist:,
either a previously pushed playlist or song is returned."""
suggest_playlist = request.GET["playlist"] == "true"
if not suggest_playlist:
if ArchivedSong.objects.count() == 0:
return HttpResponseBadRequest("No songs to suggest from")
index = random.randint(0, ArchivedSong.objects.count() - 1)
song = ArchivedSong.objects.all()[index]
return JsonResponse({"suggestion": song.displayname(), "key": song.id})
# exclude radios from suggestions
remaining_playlists = (
ArchivedPlaylist.objects.all()
.exclude(list_id__startswith="RD")
.exclude(list_id__contains="&list=RD")
)
if remaining_playlists.count() == 0:
return HttpResponseBadRequest("No playlists to suggest from")
index = random.randint(0, remaining_playlists.count() - 1)
playlist = remaining_playlists.all()[index]
return JsonResponse({"suggestion": playlist.title, "key": playlist.id})
示例2: view
# 需要导入模块: from django.http import response [as 别名]
# 或者: from django.http.response import JsonResponse [as 别名]
def view(self):
league_tag = self.request.GET.get('league')
if league_tag == 'all':
league = None
elif league_tag is not None:
league = League.objects.filter(tag=league_tag).first()
else:
league = self.league
try:
board = int(self.request.GET.get('board', ''))
except ValueError:
board = None
try:
team = int(self.request.GET.get('team', ''))
except ValueError:
team = None
return JsonResponse(_tv_json(league, board, team))
示例3: business_area_handler
# 需要导入模块: from django.http import response [as 别名]
# 或者: from django.http.response import JsonResponse [as 别名]
def business_area_handler(request, pk):
if request.method == 'POST':
data = json.loads(request.body)
if pk is None:
# New business area
BusinessArea.objects.create(name=data['name'])
return JsonResponse(ReturnStatus(True, 'OK').to_dict())
else:
# existing business area update
try:
ba = BusinessArea.objects.get(pk=pk)
ba.name = data['name']
ba.save()
except BusinessArea.DoesNotExist:
return JsonResponse(ReturnStatus(False, 'Key does not exist').to_dict())
return JsonResponse(ReturnStatus(True, 'OK').to_dict())
elif request.method == 'DELETE':
try:
ba = BusinessArea.objects.get(pk=pk)
if ba.mission_set.all().count() != 0:
return JsonResponse(ReturnStatus(False, 'Business Areas can not be deleted while missions are still associated with them.').to_dict())
ba.delete()
except BusinessArea.DoesNotExist:
return JsonResponse(ReturnStatus(False, 'Key does not exist').to_dict())
return JsonResponse(ReturnStatus(True, 'OK').to_dict())
示例4: post
# 需要导入模块: from django.http import response [as 别名]
# 或者: from django.http.response import JsonResponse [as 别名]
def post(self, request, *args, **kwargs):
email = self.request.POST.get('email')
name = self.request.POST.get('link_name')
link = self.request.POST.get('link')
if not link.endswith('/'):
link = "".join((link, "/"))
existed = m.Link.objects.filter(link=link).only(*('pk',)).count()
if existed:
return JsonResponse({'code': -1, 'msg': '链接已经在友链中!'})
OK, msg = self.is_data_valid(name, link, email)
if OK:
try:
m.Link.objects.create(link=link, email=email, link_name=name)
except:
pass
return JsonResponse({'code': 0, 'msg': '提交成功,站长审核通过即可展示'})
else:
return JsonResponse({'code': -1, 'msg': msg})
示例5: flight_list
# 需要导入模块: from django.http import response [as 别名]
# 或者: from django.http.response import JsonResponse [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
示例6: flight_detail
# 需要导入模块: from django.http import response [as 别名]
# 或者: from django.http.response import JsonResponse [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
示例7: flight_list
# 需要导入模块: from django.http import response [as 别名]
# 或者: from django.http.response import JsonResponse [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
示例8: flight_detail
# 需要导入模块: from django.http import response [as 别名]
# 或者: from django.http.response import JsonResponse [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