password dont change in views.py on django

Viewed 39

I'm trying to change a user's password on my views.py and whenever I change the password, I lose my connection and access to my account

my view.py:

 user = User.objects.get(id=request.user.id) 
 user.password = make_password(request.POST.get("password")) 
 user.save()
2 Answers

A User object normally has a .set_password(…) method [Django-doc], so you can update that password with:

request.user.set_password(request.POST['password'])
request.user.save()

You can omit fetching the user object, since request.user aleady does that. Using user = User.objects.get(id=request.user.id) is thus an extra query that only is equivalent to request.user.

Related