def post_detail(request, pk): # 根据 pk 值获取对应的 post 实例 post = get_object_or_404(Post, pk=pk) # 首先判断是否存在这个 post,不存在直接返回 404 NOT FOUND # 如果 settings.py 下的 DEBUG 属性设置为 True 的话,django 会不展示 404 页面,设置成 False 即可 if post is None: return HttpResponse(status=404) # 如果 request 是 GET 方法,则直接展示对应 pk 的 post if request.method == 'GET': serializer = PostSerializer(post) # 将序列化后的数据转换成 json 展示 return JsonResponse(serializer.data, status=200) # 如果 request 是 PUT 方法,则解析 request 中的参数, # 进行校验是否合理,合理则更新,否则返回 400 BAD REQUEST elif request.method == 'PUT': data = JSONParser().parse(request) serializer = PostSerializer(post, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) # 如果 request 是 DELETE 方法,则直接删除 elif request.method == 'DELETE': post.delete() return HttpResponse(status=204)
def put(self, request, pk, format=None): post = self.get_object(pk=pk) serializer = PostSerializer(post, data=request.data) if serializer.is_valid(): # 记得先 clear,然后判断是否上传了 id,上传了 id 需要判断是否多个 post.tags.clear() if request.data['tags']: if "," in request.data['tags']: # 我们需要提取 request.data 中 tags 所对应的值,然后通过切割字符串取出 id for i in request.data['tags'].split(","): post.tags.add(i) else: post.tags.add(request.data['tags']) serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post_list_view(request, format=None): if request.method == 'GET': posts = Post.objects.all() serializer = PostSerializer(posts, many=True) return Response(serializer.data, status=status.HTTP_200_OK) elif request.method == 'POST': serializer = PostSerializer(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 post_list(request): # 如果是 GET 请求则返回所有的列表 if request.method == "GET": posts = Post.objects.all() serializer = PostSerializer(posts, many=True) return JsonResponse(serializer.data, safe=False, status=200) # 如果是 POST 请求则保存数据 elif request.method == "POST": # 将 request 中的参数取出来进行序列化 data = JSONParser().parse(request) serializer = PostSerializer(data=data) # 判断是否有效的数据 if serializer.is_valid(): # 有效数据保存,返回 201 CREATED serializer.save() return JsonResponse(serializer.data, status=201) # 无效则返回 400 BAD_REQUEST return JsonResponse(serializer.errors, status=400)
def post_detail_view(request, pk, format=None): post = get_object_or_404(Post, pk=pk) if post is None: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = PostSerializer(post) return Response(serializer.data, status=status.HTTP_200_OK) elif request.method == 'PUT': serializer = PostSerializer(post, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': post.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def post(self, request, format=None): serializer = PostSerializer(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, format=None): posts = Post.objects.all() serializer = PostSerializer(posts, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def get(self, request, pk, format=None): serializer = PostSerializer(self.get_object(pk=pk)) return Response(serializer.data, status=status.HTTP_200_OK)