【问题标题】:django rest Invalid password format or unknown hashing algorithmdjango rest 密码格式无效或散列算法未知
【发布时间】:2020-08-07 12:10:04
【问题描述】:
User = get_user_model()

class CreateUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id','phone' , 'password',)
        write_only_fields = ('password',)

        def create(self, validated_data):
            user = User.objects.create(validated_data['phone'])
            user.set_password(validated_data['password'])
            user.save()
            return user



        })


class Register(APIView):

    def post(self, request, *args, **kwargs):
        phone = request.data.get('phone', False)
        password = request.data.get('password', False)
        print(phone)
        print(password)
        if phone and password:
            old = PhoneOTP.objects.filter(phone__iexact=phone)
            if old.exists():
                old = old.first()
                validated = old.validate
                if validated:
                    temp_data = {
                        'phone': phone,
                        'password': password
                    }
                    serializers = CreateUserSerializer(data=temp_data)
                    serializers.is_valid(raise_exception=True)
                    user = serializers.save()

                    old.delete()
                    return Response({
                        'status': True,
                        'detail': 'Account is  created '
                    })

保存用户密码字段时显示密码格式无效或哈希算法未知。 用户创建密码字段是无效的密码格式或未知的哈希算法。

无法找到 y 还在序列化程序中尝试了 user.set_unusable_password() 但相同的结果无法弄清楚

【问题讨论】:

    标签: django django-rest-framework django-forms django-views django-serializer


    【解决方案1】:

    我尝试了很多你正在使用的方法。它没有以正确的形式保存密码。 我建议你为它创建另一个模型,例如 Profile 和序列化程序,然后试试这个 -:

    views.py

    class Register(APIView):
        
        permission_classes = (AllowAny, )
        serializer_class = UserRegistrationSerializer
        def post(self, request, *args, **kwargs):
            phone = request.data.get('phone' , False)
            if phone:
                old = PhoneOTP.objects.filter(phone__iexact = phone)
                if old.exists():
                    old  = old.last()
                    validated = old.validated
    
                    if validated:
                       
                        serializer = self.serializer_class(data=request.data)
                        serializer.is_valid(raise_exception=True)
                        serializer.save()
                        response = {
                            'success' : 'True',
                            'status code' : status.HTTP_200_OK,
                            'message': 'User registered  successfully',
                            }
                        status_code = status.HTTP_200_OK
                        return Response(response, status=status_code)
            
                    else:
                        return Response({
                            'status' : False,
                            'detail' : "OTP haven't verified. FIrst do that step."
                            })
                else:
                    return Response({
                        'status' : False,
                        'detail' : 'Please verify phone number first.'
                        })
    
    
            else:
                return Response({
                    'status' : False,
                    'detail' : 'Phone password, address, Date_Of_Birth,  are not sent.'
                    })
    

    serializers.py

    class UserRegistrationSerializer(serializers.ModelSerializer):
    
        profile = ProfileSerializer(required=False)
    
        class Meta:
            model = User
            fields = ('phone', 'username', 'password', 'profile')
            extra_kwargs = {'password': {'write_only': True}}
    
        def create(self, validated_data):
            profile_data = validated_data.pop('profile')
            user = User.objects.create_user(**validated_data)
            users = Profile.objects.create(
                user=user,      
                state=profile_data['state'],
                city=profile_data['city'],
                date_Of_Birth=profile_data['date_Of_Birth'],
                address=profile_data['address']
                
            )
            users.save()
            
            return users
    

    希望答案有用

    【讨论】:

      猜你喜欢
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      • 2017-07-04
      • 2020-11-27
      • 2013-01-09
      • 2014-07-06
      • 2015-06-02
      相关资源
      最近更新 更多