rest api用于在dajngo中更改用户密码

时间:2020-07-06 17:32:20

标签: python rest django-rest-framework restapi

我想创建一个用于更改用户密码的api或他们可以更新其密码 但是我在/ Account / ChangePasswordView / 1遇到了一些问题:::: ---- NotImplementedError create()必须实现。 谢谢。

这是我的代码

views.py

@api_view(['PUT'])
def ChangePasswordView(request, user_id):
    if not request.data:
        return exception_handler.error_handling(errMsg="Invalid Access")
    old_password = request.data['old_password']
    new_password = request.data['new_password']

    if old_password == '':
        return exception_handler.error_handling(errMsg="Old password is empty.")

    if new_password == '':
        return exception_handler.error_handling(errMsg="New password is empty.")

    try:
        user = Registration.objects.get(id= user_id)
    except Registration.DoesNotExist:
        return exception_handler.error_handling(errMsg="This id does not exist")

    print(user.password)


    serializer = ChangePasswordSerializer(data=request.data)
    if serializer.is_valid():
        if not check_password(old_password,user.password):
            output = "Password is wrong"
            return exception_handler.error_handling(errMsg=output)

        else:
            serializer.save()
            return exception_handler.error_handling(status_code=201)

    else:
        output = " Fill the form as well."
        return exception_handler.error_handling(errMsg=output)

    return Response(status=status.HTTP_400_BAD_REQUEST)

code of序列化器

serializers.py
from rest_framework import serializers

class ChangePasswordSerializer(serializers.Serializer):
    model = Registration


    old_password = serializers.CharField(required=True)**strong text**
    new_password = serializers.CharField(required=True)

当我要在邮递员中运行此api时出现此错误

/ p / Account / ChangePasswordView / 1处的

NotImplementedError create()必须实现。

0 个答案:

没有答案