django-rest-framework - 错误:“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]
解决方案
我认为该方法中的代码中有错字create
。
def create(self,validated_data):
username = validated_data.get('username')
password = validated_data.get('password')
...
user.set_password(password)
...
在您的代码中,它set_password('password')
会将所有密码作为“密码”而不是实际密码
推荐阅读
- android - Android:根据字符串长度查找并设置文本视图的长度
- java - 无法加载类“org.jetbrains.kotlin.annotation.plugin.ide.AnnotationBasedPluginModelBuilderService”
- google-cloud-platform - 按服务帐号查询项目权限,无需人工准备
- azure - Azure 管道不并行运行矩阵作业
- django - django 表单和请求在同一个 http 页面中
- react-native - 致命异常:com.facebook.react.common.JavascriptException null 不是对象(评估 'n[u]')
- java - 使用 Groovy 重新映射 CSV 结构
- android - 如果我可以在 ADB 终端中看到我的 USB 连接的三星设备,但在 Mac OS 的 Android Studio 中看不到它,该怎么办?
- r - 选择R中的最后两列并通过将最后两列相除而不使用索引来形成一个新列
- java - Tomcat 找不到 Log4j 库