def list(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) serializer = self.get_serializer(queryset, many=True) return JsonResponse(data=serializer.data, code=200, msg="success", status=status.HTTP_200_OK)
def retrieve(self, request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance) return JsonResponse(data=serializer.data, code=200, msg="success", status=status.HTTP_200_OK)
def get_paginated_response(self, data): return JsonResponse(data={ "total": self.paginator.count, "data": data }, code=200, msg="success", status=status.HTTP_200_OK)
def list(self, request): # !!!记得下面这句代码很重要!!! queryset = self.filter_queryset(self.get_queryset()) serializer = serializers.CommentsSerializer(queryset, many=True) return JsonResponse(data=serializer.data, code=200, msg="success", status=status.HTTP_200_OK)
def list(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) # 实现分页 page = self.paginate_queryset(queryset) if page is not None: serializer = self.get_serializer(page, many=True) # 调用serializer.data静态方法,得到python数据类型 return self.get_paginated_response(serializer.data) serializer = self.get_serializer(queryset, many=True) return JsonResponse(data=serializer.data, code=200, msg="success", status=status.HTTP_200_OK)
def post(self, request): user = authenticate(username=request.data["username"], password=request.data["password"]) if not user: raise Http404("账号密码不匹配") login(request, user) jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) # return Response({ "success": True, "msg": "登录成功","results": token},status=status.HTTP_200_OK) return JsonResponse(data={"token": token}, code=200, msg="success", status=status.HTTP_200_OK)