def put(self, request, pk): student = self.get_object(pk) serializer = StudentSerializer(student, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def student_list(request): if request.method == 'GET': students = Student.objects.all() serializer = StudentSerializer(students, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = StudentSerializer(data=request.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 student_detail(request, pk): try: student = Student.objects.get(pk=pk) except Student.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = StudentSerializer(student) return Response(serializer.data) elif request.method == 'PUT': serializer = StudentSerializer(student, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': student.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def post(self, request): serializer = StudentSerializer(data=request.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)