0

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

authenticate(username=username, password=password)

在注册 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

用户序列化程序的代码

class UserSerializer(serializers.ModelSerializer):

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

我总是得到以下回应

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

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

4

1 回答 1

1

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

类 UserSerializer(serializers.ModelSerializer):
    元类:
        模型 = 用户
        fields = ('id','username','email','password','is_active')

    定义创建(自我,验证数据):
        用户 = super().create(validated_data)
        如果验证数据中的“密码”:
            user.set_password(validated_data['password'])
            用户保存()
        返回用户
于 2016-04-07T16:02:52.630 回答