def category_list(request): if request.method == 'GET': categories = Category.objects.all() serializer = CategorySerializer2(categories, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = CategorySerializer2(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def category_list(request): if request.method == 'GET': categories = Category.objects.all() serializer = CategorySerializer2(categories, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = CategorySerializer2(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response({'error': serializer.errors}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def category_list(request): if request.method == 'GET': categories = Category.objects.all() serializer = CategorySerializer2(categories, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = json.loads(request.body) serializer = CategorySerializer2(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors)
def category_list(request): if request.method == 'GET': categories = Category.objects.filter( name__contains='5').order_by('-id') serializer = CategorySerializer2(categories, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = CategorySerializer2(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def category_list(request): if request.method == 'GET': categories = Category.objects.all() serializer = CategorySerializer2(categories, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': body = json.loads(request.body) serializer = CategorySerializer2(data=body) if serializer.is_valid(): # create function from serializer serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors) return JsonResponse({'error': 'bad request'})
def put(self, request, pk=None): category = self.get_object(pk) serializer = CategorySerializer2(instance=category, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def put(self, request, category_id): category = self.get_object(category_id) serializer = CategorySerializer2(instance=category, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_426_UPGRADE_REQUIRED) return Response({'error': serializer.errors}, status=status.HTTP_406_NOT_ACCEPTABLE)
def put(self, request, pk): category = self.get_object(pk) serializer = CategorySerializer2(instance=category, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def put(self, request, category_id): category = self.get_object(category_id) serializer = CategorySerializer2(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 = CategorySerializer2(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response({'error': serializer.errors}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def category_detail(request, category_id): try: category = Category.objects.get(id=category_id) except Category.DoesNotExist as e: return Response({'message': str(e)}, status=400) if request.method == 'GET': serializer = CategorySerializer2(category) return Response(serializer.data) elif request.method == 'PUT': serializer = CategorySerializer2(instance=category, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors) elif request.method == 'DELETE': category.delete() return Response({'message': 'deleted'}, status=200)
def category_detail(request, pk): try: category = Category.objects.get(id=pk) except Category.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = CategorySerializer2(category) return Response(serializer.data) elif request.method == 'PUT': serializer = CategorySerializer2(instance=category, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors) elif request.method == 'DELETE': category.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def category_details(request, category_id): try: category = Category.objects.get(id=category_id) except Category.DoesNotExist as e: return Response({'error': str(e)}, status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = CategorySerializer2(category) return Response(serializer.data) elif request.method == 'PUT': serializer = CategorySerializer2(instance=category, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_202_ACCEPTED) return Response({'error': serializer.errors}, status=status.HTTP_406_NOT_ACCEPTABLE) elif request.method == 'DELETE': category.delete() return Response({'deleted': True}, status=status.HTTP_202_ACCEPTED)
def category_list(request): if request.method == 'GET': categories = TaskList.objects.all() serializer = CategorySerializer(categories, many=True) return JsonResponse(serializer.data, safe=False, status=200) elif request.method == 'POST': data = json.loads(request.body) serializer = CategorySerializer2(data=data) if serializer.is_valid(): serializer.save() # create function in serializer class return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors)
def category_detail(request, category_id): try: category = Category.objects.get(id=category_id) except Category.DoesNotExist as e: return JsonResponse({'error': str(e)}) if request.method == 'GET': serializer = CategorySerializer2(category) return JsonResponse(serializer.data) elif request.method == 'PUT': request_body = json.loads(request.body) serializer = CategorySerializer2(instance=category, data=request_body) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse({'error': serializer.errors}) # Delete selected object elif request.method == 'DELETE': category.delete() return JsonResponse({'deleted': True})
def get(self, request, category_id): category = self.get_object(category_id) serializer = CategorySerializer2(category) return Response(serializer.data)
def get(self, request): categories = Category.objects.all() serializer = CategorySerializer2(categories, many=True) return Response(serializer.data)
def post(self, request): serializer = CategorySerializer2(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 get(self, request): categories = Category.objects.filter( name__contains='5').order_by('-id') serializer = CategorySerializer2(categories, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def post(self, request): serializer = CategorySerializer2(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def get(self, request, pk=None): category = self.get_object(pk) serializer = CategorySerializer2(category) return Response(serializer.data)