Beispiel #1
0
def create_view(request):
	print(request.POST.get('title'))
	data = JSONParser().parse(request)
	serialization = PostSerializer(data=data)
	if serialization.is_valid():
		serialization.save()
		return JsonResponse(serialization.data, safe=False, status = 201)
	return JsonResponse(serialization.errors, safe=False, status=400)
Beispiel #2
0
def api_create_post_view(request):
    if request.method == "POST":
        serializer = PostSerializer(data=request.data)
        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)
Beispiel #3
0
def api_update_post_view(request, slug):
    try:
        post = Post.objects.get(slug=slug)
    except Post.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == "PUT":
        serializer = PostSerializer(post, data=request.data)
        data = {}
        if serializer.is_valid():
            serializer.save()
            data["success"] = "udpate successful"
            return Response(data=data)
        return Response(serializer.errors, status=status.HTTP_404_BBAD_REQUEST)
Beispiel #4
0
    def patch(self, request, *args, **kwargs):
        id_ = self.kwargs.get('id')

        object_to_update = Post.objects.filter(id=id_).first()

        print("object_to_update", object_to_update)
        # try:
        #     object_to_update = Post.objects.get(pk=pk)
        # except Post.DoesNotExist:
        #     object_to_update= None
        serializer = PostSerializer(object_to_update, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(code=201, data=serializer.data, safe=False)
        return JsonResponse(code=400, data="Wrong Parameters", safe=False)
Beispiel #5
0
def main_view(request, pk):
	if request.method == 'GET':
		print('GET')
		return HttpResponse('GET METHOD')

	if request.method == 'POST':
		data = JSONParser().parse(request)
		serialization = PostSerializer(data=data)
		if serialization.is_valid():
			serialization.save()
			return JsonResponse(serialization.data, safe=False, status = 201)
		return JsonResponse(serialization.errors, safe=False, status=400)
		
	if request.method == 'DELETE':
		print('DELETE')
		post = get_object_or_404(Post, pk=pk)
		# print(Post.objects.get(pk=pk))
		# Post.objects.get(pk=pk).delete()
		post.delete()
		return HttpResponse(status=204)