JWT:如何在密码或用户名错误时实现我的自定义错误消息 Django REST
JWT: How do I implement my custom error message on password or username is wrong Django REST
当用户在 Django Rest JWT 身份验证中输入错误密码时,我想实现我自己的自定义错误消息,目前默认错误消息是
"detail": "No active account found with the given credentials"
我继承了Token获取序列化器as
class TokenPairSerializer(TokenObtainSerializer):
default_error_messages = {
'login_error': _('Username or Password does not matched .')
}
@classmethod
def get_token(cls, user):
return RefreshToken.for_user(user)
@classmethod
def get_user_type(cls, user):
if user.is_superuser:
return 'super_user'
elif user.is_student:
return 'student_user'
elif user.is_teacher:
return 'teacher_user'
def validate(self, attrs):
data = super().validate(attrs)
self.validate_user()
refresh = self.get_token(self.user)
我不知道我需要在哪里覆盖错误消息以获得这样的响应
'login_error': _('Username or Password does not matched .')
任何帮助都会有所帮助。
如果只需要更改默认的错误信息,覆盖TokenObtainSerializerdefault_error_message字典,自定义no_active_account键.
源代码:here
至
class CustomTokenObtainSerializer(TokenObtainPairSerializer):
default_error_messages = {
'no_active_account': 'Username or Password does not matched.'
}
当用户在 Django Rest JWT 身份验证中输入错误密码时,我想实现我自己的自定义错误消息,目前默认错误消息是
"detail": "No active account found with the given credentials"
我继承了Token获取序列化器as
class TokenPairSerializer(TokenObtainSerializer):
default_error_messages = {
'login_error': _('Username or Password does not matched .')
}
@classmethod
def get_token(cls, user):
return RefreshToken.for_user(user)
@classmethod
def get_user_type(cls, user):
if user.is_superuser:
return 'super_user'
elif user.is_student:
return 'student_user'
elif user.is_teacher:
return 'teacher_user'
def validate(self, attrs):
data = super().validate(attrs)
self.validate_user()
refresh = self.get_token(self.user)
我不知道我需要在哪里覆盖错误消息以获得这样的响应
'login_error': _('Username or Password does not matched .')
任何帮助都会有所帮助。
如果只需要更改默认的错误信息,覆盖TokenObtainSerializerdefault_error_message字典,自定义no_active_account键.
源代码:here
至
class CustomTokenObtainSerializer(TokenObtainPairSerializer):
default_error_messages = {
'no_active_account': 'Username or Password does not matched.'
}