首页 > 解决方案 > 错误:“non_field_errors”:[“无法使用提供的凭据登录。” ]

问题描述

class ProfesRegister(serializers.ModelSerializer):
password =serializers.CharField( style={'input_type': 'password'} )
class Meta:
    model = User
    fields = ['username','password','email']

def create(self,validated_data):
    username = validated_data.get('username')
    password = validated_data.get('password')
    user = User (username=username,email=validated_data.get('email'))
    user.set_password('password')
    
    user.save()
    Token.objects.create(user=user)
    
    return user
    




INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'student',
'unit',
'universityAndProfes',

'rest_framework',
'rest_framework.authtoken',
'rest_auth',

]

urlpatterns = [
path('api/',include(router.urls)),
path('api/auth/', include('rest_auth.urls')),
path('api/register/', ProfesRegisteration.as_view() ),

]

我使用 rest auth 登录,我得到一个令牌错误!我确定当我注册时会生成我的用户令牌,但我无法登录和验证!!

这是我注册的视图

class ProfesRegisteration(generics.CreateAPIView):
serializer_class= ProfesRegister
authentication_classes = [TokenAuthentication]

标签: django-rest-frameworkdjango-rest-auth

解决方案


我认为该方法中的代码中有错字create

def create(self,validated_data):
    username = validated_data.get('username')
    password = validated_data.get('password')
    ... 
    user.set_password(password)
    ...

在您的代码中,它set_password('password')会将所有密码作为“密码”而不是实际密码


推荐阅读