0

Creating user using django rest framework, how to encrypt the user password.Need help with that. here is my view

class UserCreateAPIView(generics.CreateAPIView):

def post(self, request):
    serializer = UserSerializer(data=request.data)
    if serializer.is_valid():
        User(
            serializer.save()
        )
        return Response({"status":"sucess", "code":status.HTTP_201_CREATED, "details":serializer.data})
    return Response({"status":"unsuccessful", "code":status.HTTP_400_BAD_REQUEST, "detsils":serializer.errors})
unknown
  • 322
  • 6
  • 25
  • 1
    What do you mean to create encrypted password? Are you trying to create user and save the encrypted password in database? If yes, you should see:https://stackoverflow.com/questions/43031323/how-to-create-a-new-user-with-django-rest-framework-and-custom-user-model. If you uses, Userserializer django will save your password with encryption, you needn't worry about that. – Vidya Sagar Jan 08 '20 at 06:43
  • 1
    you can follow this answer https://stackoverflow.com/a/29391122 – Nalin Dobhal Jan 08 '20 at 06:57

1 Answers1

2

Django provides default password hashing technique using make_password method

from django.contrib.auth.hashers import make_password


print("your hashing password is  ", make_password(your password))

you can choose the different password hashing technique like md5,sha2,etc..

PASSWORD_HASHERS = (
    'myproject.hashers.MyPBKDF2PasswordHasher',
    'django.contrib.auth.hashers.PBKDF2PasswordHasher',
    'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
    'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
    'django.contrib.auth.hashers.BCryptPasswordHasher',
    'django.contrib.auth.hashers.SHA1PasswordHasher',
    'django.contrib.auth.hashers.MD5PasswordHasher',
    'django.contrib.auth.hashers.CryptPasswordHasher',
)

configure your settings.py

Dev
  • 387
  • 1
  • 12