我正在做一个使用django rest框架的项目。我创建了注册视图,没有任何问题,但在登录视图中有一个问题:当我尝试登录时,响应返回"Invalid credentials“
view.py:
class ObtainAuthTokenView(APIView):
authentication_classes = []
permission_classes = []
def post(self, request):
context = {}
email = request.POST.get('username')
password = request.POST.get('password')
account = authenticate(email=email, password=password)
if account:
try:
token = Token.objects.get(user=account)
except Token.DoesNotExist:
token = Token.objects.create(user=account)
context['response'] = 'Successfully authenticated.'
context['pk'] = account.pk
context['email'] = email.lower()
context['token'] = token.key
else:
context['response'] = 'Error'
context['error_message'] = 'Invalid credentials'
return Response(context)urls.py
urlpatterns = [
path('login', ObtainAuthTokenView.as_view(), name="login"),
]我使用这些数据来发布:
{"username":"x@example.com","password":"123456"}在我发布此数据后,响应返回“无效凭据”。我发现可能是收到请求后的邮箱和密码是一样的。
那么为什么我要面对这个问题呢?
发布于 2021-04-29 11:51:06
错误是因为我使用了错误的标识符。
我写了以下内容:
request.POST.get()而不是这样:
request.data.get()https://stackoverflow.com/questions/67303611
复制相似问题