我正在学习Django,我正在创建用于登录和注册api的休息Api它正在工作但是所有突然的登录api都无法正常工作
authenticate(username=username, password=password)
始终返回none。以下是登录代码
@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"
}
虽然之前它正在变得正确。任何帮助都感激不尽。