simplejwt双token( 三 )


自定义校验
class Student(models.Model):username = models.CharField(max_length=32)password = models.CharField(max_length=32)card = models.CharField(max_length=8)name = models.CharField(max_length=32)
class StudentLoginSerializer(serializers.Serializer):username = serializers.CharField(required=True, label="用户名")password = serializers.CharField(required=True, label="密码")def validate(self, attrs):username = attrs.get("username")password = attrs.get("password")user = models.Student.objects.filter(username=username, password=password).first()if user:refresh = RefreshToken.for_user(user)self.context["token"] = refreshreturn attrselse:raise ValidationError("用户名或密码错误")
# 登录签发tokenclass LoginViewSet(ViewSet):@action(methods=["POST"], detail=False)def login(self, request):login_ser = ser.StudentLoginSerializer(data=http://www.kingceram.com/post/self.request.data)if login_ser.is_valid():token_obj = login_ser.context["token"]return Response(data=http://www.kingceram.com/post/{"err": "", "code": 0,"access_token": str(token_obj.access_token),"refresh_token": str(token_obj)})else:return Response(data=http://www.kingceram.com/post/{"err": login_ser.errors.get("non_field_errors")[0],"code": 1,})