示例#1
0
def comment_list(request):
    data = ""
    try:
        result = comment.objects.all()
        if request.method == "GET":
            data = CommentSerializer(result, many=True)
            return JSONResponse(data.data)
        elif request.method == "POST":
            data = JSONParser().parse(request)
            serializer = CommentSerializer(data)
            if serializer.is_valid():
                serializer.save()
                return JSONResponse(serializer.data, status=201)
            return HttpResponse(status=400)
    except comment.DoesNotExist:
        print "comments empty"
    return HttpResponse(status=404)
示例#2
0
def comment_detail(request, pk):
    try:
        item = None
        if str(pk) != '0' or request.method == "GET":
            item = comment.objects.all(pk=pk)
    except comment.DoesNotExist:
        print "comment with id: %r does not exist" % item
        return HttpResponse(status=404)
    
    if request.method == 'GET':
        serializer = CommentSerializer(item)
        return JSONResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = CommentSerializer(item, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        return JSONResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        item.delete()
        return HttpResponse(status=204)