def post(self, request): data = request.data serializer = QuestionSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): data = request.data serializer = QuestionSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) return Response(serializer.errors, status=400)
def poll_details(request, id): try: instance = Question.objects.get(id=id) except Question.DoesNotExist: return JsonResponse({"error": "Given object not found"}, status=status. HTTP_404_NOT_FOUND) if request.method == 'GET': serilaizer = QuestionSerializer(instance) return JsonResponse(data=serilaizer.data, safe=False) elif request.method == 'PUT': # For parsing request which is wrong approach # data = JSONParser().parse(request) # For parsing request.body which is right approach # JSONParser().parse can be used in APIViews data = json.loads(request.body) serializer = QuestionSerializer(instance, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(data=serializer.data, status=status.HTTP_200_OK) else: JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': instance.delete() return HttpResponse(status=status.HTTP_204_NO_CONTENT)
def put(self, request, id=None): data = request.data instance = self.get_object(id) serializer = QuestionSerializer(instance, data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=200) return Response(serializer.erros, status=400)
def choice(self, request, id=None): question = sefl.get_object() data = request.data data['question'] = question.id serializer = QuestionSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) return Response(serializer.errors, status=400)
def put(self, request, id=None): instance = self.get_object(id) data = request.data serializer = QuestionSerializer(instance, data=data) if serializer.is_valid(): serializer.save() return Response(data=serializer.data, status=status.HTTP_200_OK) else: Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def poll(request): if request.method == 'GET': questions = Question.objects.all() serializer = QuestionSerializer(questions, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == "POST": json_parser = JSONParser() data = json_parser.parse(request) serializer = QuestionSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def poll(request): if request.method == 'GET': questions = Question.objects.all() serializer = QuestionSerializer(questions, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = QuestionSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse( serializer.data, status=201) # Created (Resource successfully created) return JsonResponse(serializer.errors, status=400) # Bad request
def poll(request): if request.method == 'GET': # print(type(request, "<class 'django.core.handlers.wsgi.WSGIRequest'> # ")) questions = Question.objects.all() serializer = QuestionSerializer(questions, many=True) return JsonResponse(data=serializer.data, safe=False) elif request.method == 'POST': # data = JSONParser().parse(request.body) data = json.loads(request.body) serializer = QuestionSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(data=serializer.data, status=status.HTTP_201_CREATED) else: JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def poll_details(request, id): try: instance = Question.objects.get(id=id) except Question.DoesNotExist as e: return JsonResponse( {"error": "Given question object not found."}, status=404) if request.method == "GET": serailizer = QuestionSerializer(instance) return JsonResponse(serailizer.data) elif request.method == "PUT": json_parser = JSONParser() data = json_parser.parse(request) serializer = QuestionSerializer(instance, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=200) return JsonResponse(serializer.erros, status=400) elif request.method == "DELETE": instance.delete() return HttpResponse(status=204)
def poll_details(request, id): try: instance = Question.objects.get(id=id) except Question.DoesNotExist as e: return Response({"error": "Given Question Object not found"}, status=404) if request.method == 'GET': serializer = QuestionSerializer(instance) return Response(serializer.data) elif request.method == 'PUT': data = JSONParser.parse(request) serializer = QuestionSerializer(instance, data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=200) return Response(serializer.errors, status=400) elif request.method == 'DELETE': instance.delete() return HttpResponse(status=200)
def poll(request): if request == 'GET': question = Question.objects.all() serializer = QuestionSerializer(question, many=True) return Response(serializer.data) elif request.method == 'POST': data = JSONParser.parse(request.POST) serializer = QuestionSerializer(data=data) if serializer.is_valid(): question = serializer.save() return Response(serializer.data, status=201) return Response(serializer.error, status=400)
def poll_details(request, id): try: instance = Question.objects.get(id=id) except Question.DoesNotExist: return JsonResponse({'errors': 'Given question object not found'}, status=404) if request.method == 'GET': serializer = QuestionSerializer(instance) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = QuestionSerializer(instance, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=200) # Resource successfully updated return JsonResponse(serializer.errors, status=400) # Bad request elif request.method == 'DELETE': instance.delete() return HttpResponse( status=204) # No Content (resource successfully deleted)