我正在尝试处理当用户尝试通过我的Tastypie API登录时抛出的异常,但我不确定我的语法是否正确。
def login(self, request, **kwargs):
self.method_check(request, allowed = ['post'])
data = self.deserialize(request, request.body, format = request.META.get('CONTENT_TYPE', 'application/json'))
username = data.get('email', '')
password = data.get('password', '')
print username
#print password
user = authenticate(username = username, password = password)
#member = user.member
#print user
if user:
if user.is_active:
login(request, user)
user = user.id
try:
print 'user has member'
member = user.member.id
return self.create_response(request, {'success' : True, 'member' : member})
except ObjectDoesNotExist:
print 'user does not have member'
return self.create_response(request, {'success' : True, 'member' : False})
else:
return self.create_response(request, {'success' : False, 'reason' : 'disabled'}, HttpForbidden)
else:
return self.create_response(request, {'success' : False, 'reason' : 'incorrect'}, HttpUnauthorized)
错误在member = user.member.id
的{{1}}处给出。
try
非常感谢任何帮助。
答案 0 :(得分:3)
以下行用user
属性值覆盖id
; user
现在引用long
个对象;导致AttributeError。
user = user.id
删除该行。