我正在尝试通过电子邮件链接实现帐户激活。我的User模型很简单,继承自django.contrib.auth.models.AbstractUser,所以is_active默认有字段。注册后,使用is_active=Falseparam创建新用户,我想处理案例,当用户尝试登录时,即使凭据很好,也不应该登录,因为帐户未激活。我正在使用 Knox 令牌身份验证。我的序列化器:
from django.contrib.auth import authenticate
from rest_framework import serializers, exceptions
class LoginUserSerializer(serializers.ModelSerializer):
class Meta:
model = UserModel
fields = ('username', 'password')
def validate(self, data):
user = authenticate(**data)
if user:
if user.is_active:
return user
raise exceptions.AuthenticationFailed('Account is not activated')
raise exceptions.AuthenticationFailed()
并查看:
from django.contrib.auth import login
from rest_framework.permissions import AllowAny
from rest_framework.authtoken.serializers import AuthTokenSerializer
from knox.views import LoginView
from .serializers import LoginUserSerializer
class LoginUserView(LoginView):
serializer_class = LoginUserSerializer
permission_classes = [AllowAny]
def post(self, request, *args, **kwargs):
serializer = AuthTokenSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
login(request, user)
return super(LoginUserView, self).post(request)
使用该代码,我偶然发现了问题:当我尝试使用已激活的帐户登录时,一切看起来都很好,但是当我尝试未激活的帐户时Account is not activated,我得到:
{
"non_field_errors": [
"Unable to log in with provided credentials."
]
}
我认为,与其说是序列化程序,不如说是来自视图。
一只名叫tom的猫
相关分类