Example #1
0
def snippet_detail(request, pk):
    """
    获取、删除或更新一个snippet实例
    :param request: 请求对象
    :param pk: id主键
    :return:
    """
    try:
        snippet = Snippets.objects.get(pk=pk)
    except Snippets.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = SnippetSerializer2(snippet)

        return JSONResponse(serializer.data, status=201)

    elif request.method == 'PUT':
        # 从请求中解析出数据进行修改
        data = JSONParser().parse(request)
        serializer = SnippetSerializer2(snippet, data=data)

        if serializer.is_valid():
            serializer.save()

            return JSONResponse(serializer.data)

        return JSONResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        snippet.delete()

        return HttpResponse(status=204)
Example #2
0
def snippet_detail1(request, pk, format=None):
    """
    获取、删除或更新一个snippet实例
    :param request: 请求对象
    :param pk: id主键
    :param format: 数据格式
    :return:
    """
    try:
        snippet = Snippets.objects.get(pk=pk)
    except Snippets.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = SnippetSerializer2(snippet)

        return Response(serializer.data, status=status.HTTP_200_OK)

    elif request.method == 'PUT':
        # 从请求中解析出数据进行修改
        data = JSONParser().parse(request)
        serializer = SnippetSerializer2(snippet, data=data)

        if serializer.is_valid():
            serializer.save()

            return Response(serializer.data, status=status.HTTP_200_OK)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        snippet.delete()

        return Response(status=status.HTTP_204_NO_CONTENT)
Example #3
0
    def put(self, request, pk, format=None):
        # data = JSONParser().parse(request)
        data = request.data
        snippet = self.get_object(pk)
        serializer = SnippetSerializer2(snippet, data=data)

        if serializer.is_valid():
            serializer.save()

            return Response(serializer.data)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #4
0
    def post(self, request, format=None):
        # JSON直接解析请求对象得到数据
        data = JSONParser().parse(request)
        # 将数据序列化,并验证
        serializer = SnippetSerializer2(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)
Example #5
0
def snippet_list1(request, format=None):
    """
    列出所有的code snippet,或创建一个新的snippet。
    """
    if request.method == 'GET':
        snippets = Snippets.objects.all()

        # 将 queryset 序列化后返回给用户,serializer.data是一个OrderedDict()
        # 将 单个实例 序列化后返回给用户,serializer.data是一个Dict()
        serializer = SnippetSerializer2(snippets, many=True)

        return Response(serializer.data)

    elif request.method == 'POST':
        # JSON直接解析请求对象得到数据
        data = JSONParser().parse(request)
        # 将数据序列化,并验证
        serializer = SnippetSerializer2(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)
Example #6
0
    def get(self, request, pk, format=None):
        snippet = self.get_object(pk)
        serializer = SnippetSerializer2(snippet)

        return Response(serializer.data, status=status.HTTP_200_OK)
Example #7
0
    def get(self, request, format=None):
        snippets = Snippets.objects.all()
        serializer = SnippetSerializer2(snippets, many=True)

        return Response(serializer.data, status=status.HTTP_200_OK)