Python django.http.response.JsonResponse() Examples
The following are 30
code examples of django.http.response.JsonResponse().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.http.response
, or try the search function
.
Example #1
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #2
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #3
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #4
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #5
Source File: views.py From ishare with MIT License | 6 votes |
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})
Example #6
Source File: views.py From yats with MIT License | 6 votes |
def yatse_api(request): try: if request.method != 'PROPFIND': api_login(request) except PermissionDenied: return HttpResponseForbidden(request.META.get('HTTP_API_USER')) if request.method == 'PROPPATCH': data = json.loads(request.body) if 'ticket' in data and 'method' in data: if data['method'] == 'notify': tickets_participants.objects.filter(ticket=data['ticket'], user=request.user).update(seen=True) return HttpResponse('OK') return HttpResponseNotFound('invalid method\n\n%s' % request.body) elif request.method == 'PROPFIND': fields = buildYATSFields([]) return JsonResponse(fields[0], safe=False) elif request.method == 'SEARCH': return JsonResponse(YATSSearch(request), safe=False) else: return HttpResponseNotFound('invalid method')
Example #7
Source File: views.py From dart with Apache License 2.0 | 6 votes |
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())
Example #8
Source File: views.py From heltour with MIT License | 6 votes |
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))
Example #9
Source File: views.py From django-mfa with MIT License | 6 votes |
def index(request): if request.user and request.user.is_authenticated: return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) if request.method == 'POST': form = LoginForm(request.POST, request.FILES) if form.is_valid(): user = form.user if is_u2f_enabled(user): request.session['u2f_pre_verify_user_pk'] = user.pk request.session['u2f_pre_verify_user_backend'] = user.backend login(request, form.user) return JsonResponse({"error": False}) else: return JsonResponse({"error": True, "errors": form.errors}) context = { "registration_form": RegistrationForm, "login_form": LoginForm } return render(request, 'login.html', context)
Example #10
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #11
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #12
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #13
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #14
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #15
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #16
Source File: suggestions.py From raveberry with GNU Lesser General Public License v3.0 | 6 votes |
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})
Example #17
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #18
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #19
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #20
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #21
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #22
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #23
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #24
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #25
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #26
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #27
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #28
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #29
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)
Example #30
Source File: views.py From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License | 6 votes |
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)