Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用令牌django rest框架登录_Python_Django_Api - Fatal编程技术网

Python 使用令牌django rest框架登录

Python 使用令牌django rest框架登录,python,django,api,Python,Django,Api,我正在使用django rest框架进行一个项目。我创建注册视图没有任何问题,但在登录视图中我有一个问题: 当我尝试登录时,响应返回“无效凭据” view.py: class ObtainAuthTokenView(APIView): authentication_classes = [] permission_classes = [] def post(self, request): context = {} email = requ

我正在使用django rest框架进行一个项目。我创建注册视图没有任何问题,但在登录视图中我有一个问题: 当我尝试登录时,响应返回“无效凭据”

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)
url.py

urlpatterns = [
    path('login', ObtainAuthTokenView.as_view(), name="login"),
]
我使用这些数据发布:

{"username":"x@example.com","password":"123456"}
发布此数据后,响应返回“无效凭据”。 我发现收到请求后的电子邮件和密码可能是相等的


那么我为什么要面对这个问题呢?

错误是因为我使用了错误的标识符

我写道:

request.POST.get()
与此相反:

request.data.get()