【问题标题】:Tastypie resources.py attribute error. request.method.lower()Tastypie resources.py 属性错误。 request.method.lower()
【发布时间】:2014-10-26 15:32:26
【问题描述】:

当我尝试对资源 URL(127.0.0.1:8000/api/v1/user/login/) 使用 POST 或 GET 方法时,出现以下错误。

Traceback(最近一次调用最后一次): 文件“/home/mithu/pipliko/pipliko/load_projects/api.py”,第 55 行,登录 self.method_check(self,allowed=['post','get']) 文件“/home/mithu/pipliko/local/lib/python2.7/site-packages/tastypie/resources.py”,第 519 行,在 method_check request_method = request.method.lower() getattr 中的文件“/home/mithu/pipliko/local/lib/python2.7/site-packages/tastypie/resources.py”,第 186 行 引发 AttributeError(name) 属性错误:方法

我的 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 login(self,request,**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);

【问题讨论】:

    标签: django python-2.7 tastypie


    【解决方案1】:

    您以不正确的方式使用 method_check。尝试使用 request 而不是 self:

    self.method_check(request, ['post','get'])

    文档:http://django-tastypie.readthedocs.org/en/latest/resources.html#method-check

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-17
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      相关资源
      最近更新 更多