身份验证失败的 django rest 框架

Authentication failed django rest framework

我正在学习 Django,我正在为登录和注册创建休息 Api api 它正在工作,但突然登录 api 不工作

authenticate(username=username, password=password)

总是返回 none 而注册 api 正常工作。下面是登录代码

@api_view(['POST'])
def login(requset):
    """
    This api for user login
    ---
    parameters:
    - name: username
      description: username for login
      required: true
      type: string
      paramType: form
    - name: password
      required: true
      type: string
      paramType: form
    consumes:
        - application/json
        - application/xml
    produces:
        - application/json
        - application/xml
    """
    username = requset.data.get('username')
    print(username)
    password = requset.data.get('password')
    print(password)
    user = authenticate(username=username, password=password)
    if user is not None:
        response_data = {}
        response_data['result'] = 'success'
        response_data['message'] = 'Login successful'
        # response_data['auth_token'] = auth_token

        return HttpResponse(json.dumps(response_data),
                            content_type="application/json")
    else:
        response_data = {}
        response_data['result'] = 'error'
        response_data['message'] = 'Login Unsuccessful'
        return HttpResponse(json.dumps(response_data),
                                     content_type="application/json")

这是注册码

class Register(generics.CreateAPIView):
    """
    Your docs
    ---
    type:
      username:
        required: true
        type: string
      password:
        required: true
        type: url
      email:
        required: true
        type: string

    serializer: .serializers.UserSerializer
    omit_serializer: false

    parameters_strategy: merge
    omit_parameters:
        - path
    parameters:
        - name: username
          description: Username for registration purpose
          required: true
          type: string
          paramType: form
        - name: password
          description: * password for registration purpose
          required: true
          type: string
          paramType: form
        - name: email
          description: * email for registration purpose
          required: true
          type: string
          paramType: form

    responseMessages:
        - code: 401
          message: Not authenticated

    consumes:
        - application/json
        - application/xml
    produces:
        - application/json
        - application/xml
    """

    #first define query set
    queryset = User.objects.all()
    #define model
    model = User
    #define serializer
    serializer_class = UserSerializer

userserializer 代码

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('id','username','email','password','is_active')

我总是得到低于响应

{
  "message": "Login Unsuccessful",
  "result": "error"
}

虽然之前它是正确的。任何帮助都感激不尽。

如评论中所述,如果您只是在用户序列化程序中设置密码,您的密码将以明文形式保存在 Django 需要哈希的位置。所以你需要做这样的事情

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id','username','email','password','is_active')

    def create(self, validated_data):
        user = super().create(validated_data)
        if 'password' in validated_data:
            user.set_password(validated_data['password'])
            user.save()
        return user