简体   繁体   English

如何使用 django rest 框架对用户进行身份验证

[英]How to authenticate a user using django rest framework

I am trying to authenticate a user using django rest framework and authenticate import below but for some reason i keep getting null as a result when i check the front end with reactJS , i am referring to the variable logged_in_used below:我正在尝试使用django rest 框架对用户进行authenticate ,并在下面对导入进行authenticate ,但由于某种原因,当我使用reactJS检查前端时,我一直得到null结果,我指的是下面的变量logged_in_used

from django.contrib.auth import authenticate

...

if request.method == "POST":
        username = request.data.get('username')
        password = request.data.get('password')

        get_user = User.objects.get(username=username)
        logged_in_used = authenticate(username=get_user.username, password=get_user.check_password(password))

         ...
         # data with token dictionnary
         ...

         result = (data_with_token, logged_in_used, status.HTTP_200_OK)

Is there something that i am doing wrong here?我在这里做错了什么吗?

try it like this像这样试试

from django.contrib.auth import authenticate, login

...

if request.method == "POST":
    username = request.POST['username']
    password = request.POST['password']

    logged_user = authenticate(request, username=username, password=password)

    if logged_user is not None:
        login(request, logged_user)

from here you can redirect or whatever else:)从这里你可以重定向或其他任何东西:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM