Sorry im new to Django, I have weird case where function not returning anything that the function suppose to return Response rest framework to clientt.
class AccountValidationView(APIView):
def put(self, request):
token = str(request.POST.get('token')).strip()
token = token.split('.')
password = str(request.POST.get('password'))
confirm_password = str(request.POST.get('confirm_password')).strip()
validation_password(password, confirm_password)
# if valid process data validation
def validation_password(password, confirm):
if len(password) == 0:
message = {
'password': 'Password cannot be empty'
}
return Response(message, status=status.HTTP_400_BAD_REQUEST)
if password != confirm:
message = {
'confirm_password': 'Confirm password not match with the password you given'
}
return Response(message, status=status.HTTP_400_BAD_REQUEST)
the validation_password function not returning anything so is straight to processing data. what do I do wrong here. My validation_password not returning response when error occurred.
Okay, so first thing in your def put(self, request): only validation_password is returning the Response what if it passes validation, have you return anything.
Also can you share your ajax method as well.
No need to remove, You are right if server sends the response to client then it should be printed in you browser’s console.
Its straight to get response from serializer that save the input. If password and confirm password not equal it suppose to return :
{'confirm_password':'Confirm password not match with the password you given'}
But it get save to database. That what im looking for right now. It suppose to return the error message on validation_password() function if error occured. But it didnt.
Okay, so I’ve updated your view and validation method
def put(self, request):
...
password = request.data['password']
confirm_password = request.data['confirm_password']
message = validation_password(password, confirm_password)
if message:
return Response(message, status=status.HTTP_400_BAD_REQUEST)
# Other code that you want to execute after validation_password
def validation_password(password, confirm):
message = None
if len(password) == 0:
message = {
'password': 'Password cannot be empty'
}
return message
if password != confirm:
message = {
'confirm_password': 'Confirm password not match with the password you given'
}
return message
As much I know, it is because in this def validation_password(password, confirm) method request is not given and to return a response or to get a request from client it is required.
Whereas in def put(self, request) this method request is being passed and after checking here that a message have anything Response was returned.