Return 响应语句未返回任何响应
Return response statement is not returning any response
我正在创建注销视图以使用 simplejwt 从我的 django restframework 注销。没有直接注销的方法,因此只能将刷新令牌列入黑名单。
那些打印语句按预期工作,所以它确实将标记列入黑名单,但 return 语句没有 return 任何东西,为什么会这样,我如何 return 回应?我我猜保存功能没有 return 任何东西,是真的吗?
class LogoutSerializer(serializers.Serializer):
refresh = serializers.CharField()
def validate(self, attrs):
self.token = attrs['refresh']
return attrs
def save(self, **kwargs):
try:
RefreshToken(self.token).blacklist()
print('done')
return Response({'msg':'token has been blacklisted'})
except TokenError:
print('not done')
return Response({'msg':'token is expired or blacklisted'})
views.py
class LogoutAPIView(APIView):
serializer_class = LogoutSerializer
permission_classes = [IsAuthenticated]
def post(self, request):
serializer = self.serializer_class(data = request.data)
serializer.is_valid(raise_exception = True)
serializer.save()
return Response(status = status.HTTP_204_NO_CONTENT)
def post(self, request):
serializer = self.serializer_class(data = request.data)
serializer.is_valid(raise_exception = True)
serializer.save()
return Response(status = status.HTTP_204_NO_CONTENT) # this is the response that you're actually returning to the client.
如果您想 return 序列化程序中的响应,您可以这样做:
class LogoutSerializer(serializers.Serializer):
refresh = serializers.CharField()
def validate(self, attrs):
self.token = attrs['refresh']
return attrs
def save(self, **kwargs):
RefreshToken(self.token).blacklist()
然后:
def post(self, request):
serializer = self.serializer_class(data = request.data)
serializer.is_valid(raise_exception = True)
try:
serializer.save()
return Response({'msg':'token has been blacklisted'})
except TokenError:
return Response({'msg':'token is expired or blacklisted'})
我正在创建注销视图以使用 simplejwt 从我的 django restframework 注销。没有直接注销的方法,因此只能将刷新令牌列入黑名单。
那些打印语句按预期工作,所以它确实将标记列入黑名单,但 return 语句没有 return 任何东西,为什么会这样,我如何 return 回应?我我猜保存功能没有 return 任何东西,是真的吗?
class LogoutSerializer(serializers.Serializer):
refresh = serializers.CharField()
def validate(self, attrs):
self.token = attrs['refresh']
return attrs
def save(self, **kwargs):
try:
RefreshToken(self.token).blacklist()
print('done')
return Response({'msg':'token has been blacklisted'})
except TokenError:
print('not done')
return Response({'msg':'token is expired or blacklisted'})
views.py
class LogoutAPIView(APIView):
serializer_class = LogoutSerializer
permission_classes = [IsAuthenticated]
def post(self, request):
serializer = self.serializer_class(data = request.data)
serializer.is_valid(raise_exception = True)
serializer.save()
return Response(status = status.HTTP_204_NO_CONTENT)
def post(self, request):
serializer = self.serializer_class(data = request.data)
serializer.is_valid(raise_exception = True)
serializer.save()
return Response(status = status.HTTP_204_NO_CONTENT) # this is the response that you're actually returning to the client.
如果您想 return 序列化程序中的响应,您可以这样做:
class LogoutSerializer(serializers.Serializer):
refresh = serializers.CharField()
def validate(self, attrs):
self.token = attrs['refresh']
return attrs
def save(self, **kwargs):
RefreshToken(self.token).blacklist()
然后:
def post(self, request):
serializer = self.serializer_class(data = request.data)
serializer.is_valid(raise_exception = True)
try:
serializer.save()
return Response({'msg':'token has been blacklisted'})
except TokenError:
return Response({'msg':'token is expired or blacklisted'})