def put(self, request, course_id): category = self.get_object(course_id) serializer = CourseSerializer(instance=category, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response({'error': serializer.errors})
def post( self, request, ): serializer = CourseSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, 201) return Response({'error': serializer.errors}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def courseApi(request, id=0): if request.method == 'GET': course = Courses.objects.all() course_serializer = CourseSerializer(course, many=True) return JsonResponse(course_serializer.data, safe=False) elif request.method == 'POST': course_data = JSONParser().parse(request) course_serializer = CourseSerializer(data=course_data) if course_serializer.is_valid(): course_serializer.save() return JsonResponse("Added Successfully!", safe=False) return JsonResponse("Failed to add", safe=False) elif request.method == 'PUT': course_data = JSONParser().parse(request) course = Courses.objects.get(CourseId=course_data['CourseId']) course_serializer = CourseSerializer(course, data=course_data) if course_serializer.is_valid(): course_serializer.save() return JsonResponse("Updated Successfully!", safe=False) return JsonResponse("Failed to update", safe=False) elif request.method == 'DELETE': course = Courses.objects.get(CourseId=id) course.delete() return JsonResponse("Deleted Successfully!", safe=False)
def post(self, request, *args, **kwargs): ser = CourseSerializer(data=request.data) if ser.is_valid(): ser.save() return Response('添加成功') return Response(ser.errors)
def post(self, request, format=None): serializer = CourseSerializer(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)