Django KeyError: 'password'

Viewed 45

I am trying to update my user profile. But at the 'if data['password'] != '':' line, it's showing a KeyError at 'password'. I am understanding the data I called at request.data doesn't contain 'password', which is not making any sense. Please help me out, someone.

@api_view(['PUT'])
@permission_classes([IsAuthenticated])
def updateUserProfile(request):
    user = request.user
    serializer = UserSerializerWithToken(user, many=False)

    data = request.data
    user.first_name = data['name']
    user.username = data['email']
    user.email = data['email']
    if data['password'] != '':
        user.password = make_password(data['password'])

    user.save()

    return Response(serializer.data)
1 Answers

The KeyError is triggered because it is most likely that it is not arriving in your Request and you are trying to access data that does not exist, it is also more recommended to do it this way Example:

data.get("name",None)

you can validate if the data is traveling in your request with the

print(request.data)
Related