def put(self, request, pk): post = self.get_object(pk) serializer = PostSerializer(post, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def view_post(self, post): views = post.views + 1 post_serializer = PostSerializer(post, data={'views': views}, partial=True) if post_serializer.is_valid(): post_serializer.save()
def post(self, request): serializer = PostSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(response_data(True, serializer.data)) else: return Response(response_data(False, serializer.errors), status=status.HTTP_400_BAD_REQUEST)
def unlike_post(self, pk): post = PostDetailAPIView().get_object(pk) like = post.like - 1 post_serializer = PostSerializer(post, data={'like': like}, partial=True) if post_serializer.is_valid(): post_serializer.save()
def post(self, request): print('post') print(request.data) serializer = PostSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) return Response(serializer.data, status=400)
def put(self, request, pk): post = self.get_object(pk) serializer = PostSerializer(post, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(response_data(True, serializer.data)) else: return Response(response_data(False, serializer.errors), status=status.HTTP_400_BAD_REQUEST)
def index(request, pageInfo_id): """ GET | category에 따라서 board 리스트를 보여줍니다. POST | 게시글을 등록합니다. """ if request.method == "GET": posts = get_list_or_404(Post, pageInfo=pageInfo_id) serializer = PostSerializer(posts, many=True) return Response(serializer.data, status=status.HTTP_200_OK) elif request.method == "POST": # 게시글 등록 serializer = PostSerializer(data=request.data) if serializer.is_valid(raise_exception=True): #is_valid()에서 비속어 처리도 진행 serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)