Example #1
0
 def put(self, request, pk, format=None):
     post = self.get_object(pk)
     serializer = BlogModelSerializer(post, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.error, status=status.HTTP_400_BAD_REQUEST)
Example #2
0
def blog_detail(request, pk):
    """try:
        post = Blog.objects.get(pk=pk)
    except Blog.DoesNotExist:
        return HttpResponse(status=404)"""

    if request.method == 'GET':
        try:
            post = Blog.objects.get(pk=pk)
        except Blog.DoesNotExist:
            return HttpResponse(status=404)
        serializer = BlogModelSerializer(post)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'PUT':
        try:
            post = Blog.objects.get(pk=pk)
        except Blog.DoesNotExist:
            return HttpResponse(status=404)
        data = JSONParser().parse(request)
        serializer = BlogModelSerializer(post, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.error, status=400)

    elif request.method == 'DELETE':
        try:
            post = Blog.objects.get(pk=pk)
        except Blog.DoesNotExist:
            return HttpResponse(status=404)
        post.delete()
        return HttpResponse(status=204)
Example #3
0
def blog_list(request):
    if request.method == 'GET':
        posts = Blog.objects.all()
        serializer = BlogModelSerializer(posts, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = BlogModelSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.error, status=400)
Example #4
0
def post_list(request, format=None):
    """List all blog post or create new one"""
    """We can return multiple type response using format as xml, json
    we just have to call as /blog/4.json/"""
    if request.method == 'GET':
        posts = Blog.objects.all()
        serializer = BlogModelSerializer(posts, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = BlogModelSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.error, status=status.HTTP_404_NOT_FOUND)
Example #5
0
def post_detail(request, pk, format=None):
    """detail for a single post"""
    try:
        post = Blog.objects.get(pk=pk)
    except Blog.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = BlogModelSerializer(post)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = BlogModelSerializer(post, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.error, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        post.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Example #6
0
 def get(self, request, pk, format=None):
     post = self.get_object(pk)
     serializer = BlogModelSerializer(post)
     return Response(serializer.data, status=status.HTTP_200_OK)
Example #7
0
 def post(self, request, format=None):
     serializer = BlogModelSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.error, status=status.HTTP_400_BAD_REQUEST)
Example #8
0
 def get(self, request, format=None):
     posts = Blog.objects.all()
     serializer = BlogModelSerializer(posts, many=True)
     return Response(serializer.data)