修复判断验证码是否存在的时候,用户不存在导致的报错

This commit is contained in:
virusdefender
2015-09-19 18:46:03 +08:00
parent 18abd6a465
commit 83124f3c86
2 changed files with 7 additions and 9 deletions

View File

@@ -26,17 +26,15 @@ class UserLoginAPIView(APIView):
serializer = UserLoginSerializer(data=request.data) serializer = UserLoginSerializer(data=request.data)
if serializer.is_valid(): if serializer.is_valid():
data = serializer.data data = serializer.data
user = User.objects.get(username=data["username"]) user = auth.authenticate(username=data["username"], password=data["password"])
# 只有管理员才适用验证码登录 # 用户名或密码错误的话 返回None
if user:
if user.admin_type > 0: if user.admin_type > 0:
if not "captcha" in data: if "captcha" not in data:
return error_response(u"请填写验证码!") return error_response(u"请填写验证码!")
captcha = Captcha(request) captcha = Captcha(request)
if not captcha.check(data["captcha"]): if not captcha.check(data["captcha"]):
return error_response(u"验证码错误") return error_response(u"验证码错误")
user = auth.authenticate(username=data["username"], password=data["password"])
# 用户名或密码错误的话 返回None
if user:
auth.login(request, user) auth.login(request, user)
return success_response(u"登录成功") return success_response(u"登录成功")
else: else: