我正在学习 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"
}
虽然以前它是正确的。任何帮助都感激不尽。