[Django]-How to use check_password function in django

2👍

check_password does not work like this. To make it work, you need to use Django’s own authentication system. If you are concerned about using pbkdf2_sha256, Django provides this hasher. To use this with you own auth system, add it to settings:

PASSWORD_HASHERS = [
    'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
    'django.contrib.auth.hashers.PBKDF2PasswordHasher',
    'django.contrib.auth.hashers.Argon2PasswordHasher',
]

And in django authentication, you do not need to hash it manually, django will take care of it itself. All you need to do is save the user like this:

from django.contrib.auth.models import User

user = User.objects.create_user(username=username, email=email, password=password, #.. other required fields)

And to check password:

user = User.objects.get(username=username)
user.check_password(password)

More information can be found in documentation

👤ruddra

4👍

You can do:

if check_password(password_user_entered, request.user.password):
    # You can authenticate

Here, password_user_entered is password that came from the request(or, pw to be checked). And, request.user.password which is the password with which we want to compare.

Leave a comment