简体   繁体   中英

how do i manually authenticate and log in a user in django?

    if request.method=='POST':
        try:        
            email=request.POST['email']
            password = request.POST['password']
            if StudentUser.objects.filter(email=email).exists():
                user = StudentUser.objects.get(email=email)
                if user.check_password(password):
                    user = auth.authenticate(email=email)
                    if user is not None:
                        auth.login(request,user)
                        messages.success(request,'Successfully Loggedin')
                        return redirect('/')
                else:
                    messages.warning(request,'Password does not match')
                    return redirect('login')
            else:
                messages.error(request,'No Account registered with this mail')
                return redirect('login')
        except Exception as problem:
            messages.error(request,problem)
            return redirect('login')
    return render(request,'login.html')

This above code i am trying to authenticate the user manually, but it is not working. i want to authenticate a user by manually checking the password. How can i do it?

** when I am passing the password in auth.authenticate function it is showing password does not match error

You should pass password as argument to authenticate method

From docs

user = authenticate(username='john', password='secret')

Use authenticate() to verify a set of credentials. It takes credentials as keyword arguments, username and password for the default case, checks them against each authentication backend, and returns a User object if the credentials are valid for a backend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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