def post(self, request): serializer = JournalistSerializer(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 put(self, request, pk): journalist = self.get_object(pk) serializer = JournalistSerializer(journalist, 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 put(self, request, pk): journalist = get_object_or_404(Journalist, pk=pk) serializer = JournalistSerializer(journalist, data=request.data, context={'request': request}) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serializer = JournalistSerializer(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) # @api_view(["GET", "POST"]) # def article_list_create_api_view(request): # if request.method == "GET": # articles = Article.objects.filter(is_active=True) # serializer = ArticleSerializer(articles, many=True) # return Response(serializer.data) # elif request.method == "POST": # serializer = ArticleSerializer(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) # @api_view(["GET", "PUT", "DELETE"]) # def article_detail_api_view(request, pk): # try: # article = Article.objects.get(pk=pk) # except Article.DoesNotExist: # return Response({ # "error": { # "code": 404, # "message": "Article not found!" # } # }, status=status.HTTP_404_NOT_FOUND) # if request.method == "GET": # serializer = ArticleSerializer(article) # return Response(serializer.data) # elif request.method == "PUT": # serializer = ArticleSerializer(article, 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": # article.delete() # return Response(status=status.HTTP_204_NO_CONTENT)
def post(self, request): serializer = JournalistSerializer(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) # @api_view(['GET', 'POST']) # def article_list_create_api_view(request): # if request.method == 'GET': # articles = Article.objects.filter(active=True) # serializer = ArticleSerializer(articles, many=True) # return Response(serializer.data) # elif request.method == 'POST': # serializer = ArticleSerializer(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) # @api_view(['GET','PUT','DELETE']) # def article_detail_api_view(request,pk): # try: # article = Article.objects.get(pk=pk) # except Article.DoesNotExist : # return Response({'error': { # 'code': 404, # 'message': 'Article not found' # }}, status=status.HTTP_404_NOT_FOUND) # if request.method == 'GET': # serializer = ArticleSerializer(article) # return Response(serializer.data) # elif request.method == 'PUT': # serializer = ArticleSerializer(article, data=request.data) # if serializer.is_valid(): # serializer.save() # return Response(serializer.data, status=status.HTTP_200_OK) # elif request.method == 'DELETE': # article.delete() # return Response(status=status.HTTP_204_NO_CONTENT)
def post(self, request): serializer = JournalistSerializer(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) # class JournalistListCreateAPIView(APIView): # def get(self, request): # journalists = Journalist.objects.all() # serializer = JournalistSerializer(journalists, # many=True, # context={'request': request}) # return Response(serializer.data) # def post(self, request): # serializer = JournalistSerializer(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)