[Fixed]-Django: set_password isn't hashing passwords?

41👍

set_password only creates a hashed password; it doesn’t save the value in the database. Call save() to actually save it.


In your views, it should be

user.save()

below the line

user.set_password(user.password)

You didn’t write the brackets (parentheses). That’s why save method is not being called after you hash the password.

👤xyres

6👍

user.set_password(user.password)
user.save()

2👍

answer update 25 july 2021

set_password method working django version 3.2

custom singup django 3.2

make sure the variable not passing null. for development you can make print before pass to set_password method.

def user_register(request):

  if (request.method == 'POST'):
        username = request.POST.get('username')
      
        password = request.POST.get('password')
  print(password, type(password))
  #make sure the password not passed null
  user = User.objects.create_user(
            email=email,
            name=username,  
            password=password,
        )      
  user.set_password(password)
  user.save()

how you test with django shell.

python manage.py shell

here list django shell commands


from .models import user_type, User

user = User.objects.create_user(email="ll55@gmail.com",password="uU123456",name="le5")
user.set_password("uU123456")
user.save()



u = User.objects.get(name="as6")
u.name
u.password

Leave a comment