How to hash a password in django while creating a user
here is my views from django project. how do i hash my password
@api_view(['POST'])
def register(request):
data = request.data
serializer = SignUpSerializers(data=data)
if serializer.is_valid():
if not CustomUser.objects.filter(email=data['email']).exists():
user = CustomUser.objects.create(
first_name = data['first_name'],
last_name = data['last_name'],
email = data['email'],
username = data['username'],
password = data['password']
)
return Response({
'details':"User registered sucessfully."
}, status.HTTP_201_CREATED)
return Response({
'error':"Email already exists."
}, status.HTTP_400_BAD_REQUEST)
return Response(serializer.errors)
hasing while creating a user is better or while saving in model
You can hash your password by using make_password
from django.contrib.auth.hashers import make_password
@api_view(['POST'])
def register(request):
data = request.data
serializer = SignUpSerializers(data=data)
if serializer.is_valid():
if not CustomUser.objects.filter(email=data['email']).exists():
user = CustomUser.objects.create(
first_name = data['first_name'],
last_name = data['last_name'],
email = data['email'],
username = data['username'],
password = make_password(data['password'])
)
return Response({
'details':"User registered sucessfully."
}, status.HTTP_201_CREATED)
return Response({
'error':"Email already exists."
}, status.HTTP_400_BAD_REQUEST)
return Response(serializer.errors)