当我尝试POST或GET方法到资源URL(127.0.0.1:8000/api/v1/user/login/)时,我得到以下错误。
追踪(最近一次通话): 文件" /home/mithu/pipliko/pipliko/load_projects/api.py",第55行,登录 self.method_check(个体,允许= ['后''获得']) 文件" /home/mithu/pipliko/local/lib/python2.7/site-packages/tastypie/resources.py",第519行,在method_check中 request_method = request.method.lower() 文件" /home/mithu/pipliko/local/lib/python2.7/site-packages/tastypie/resources.py" ;,第186行, getattr 引发AttributeError(名称) AttributeError:方法
我的api.py
def prepend_urls(self):
return [
url(r"^(?P<users>%s)/login%s$" %
(self._meta.resource_name, trailing_slash()),
self.wrap_view('login'), name="api_login"),
url(r'^(?P<users>%s)/logout%s$' %
(self._meta.resource_name, trailing_slash()),
self.wrap_view('logout'), name='api_logout'),
]
def登录(自我,请求,** kwargs):
try:
print request.method
self.method_check(self,allowed=['post','get'])
except Exception,e:
print traceback.format_exc()
#or
print sys.exc_info()[0]
data = self.deserialize(request,request.raw_post_data,format = request.META.get('CONTENT_TYPE', 'application/json'))
username = data.get('username','')
password = data.get('password','')
authenticKey = authenticate(username = username,password = password)
if authenticKey:
if authenticKey.is_active:
login(request,authenticKey)
return self.create_response(request,{'success':False, 'user':{'id':user.id}})
else:
return self.create_response(request,{'success':False,'reason':'Account disabled'},HttpForbidden);
else:
return self.create_response(request,{'success':False,'reason':'Wrong credentials'},HttpUnauthorized);
答案 0 :(得分:0)
您以不正确的方式使用method_check。尝试使用请求而不是自己:
self.method_check(request, ['post','get'])
文档:http://django-tastypie.readthedocs.org/en/latest/resources.html#method-check