WebApp2 - self.request返回一个NoneType对象

时间:2015-07-06 20:08:26

标签: python webapp2

我似乎无法深究这一点。

我有一个继承自webapp2的RequestHandler的类:

class MainHandler(webapp2.RequestHandler):

    def set_secure_cookie(self, name, val):
        cookie_val = make_secure_val(val)
        self.response.headers.add_header(
        'Set-Cookie',
        '%s=%s; Path=/' % (name, cookie_val))

    def read_secure_cookie(self, name):
        cookie_val = self.request.cookies.get(name)
        return cookie_val and check_secure_val(cookie_val)

    def login(self, user):
        self.set_secure_cookie('user_id', str(user.key().id()))

    def logout(self):
        self.response.headers.add_header('Set-Cookie', 'user_id=; Path=/')

    def initialize(self, *a, **kw):
        webapp2.RequestHandler.initialize(self, *a, **kw)
        uid = self.read_secure_cookie('user_id')
        self.user = uid and User.by_id(int(uid))

我在登录时设置了cookie,但是当我尝试访问read_secure_cookie时,我得到以下回复:

  File "/Users/admin/Desktop/coreator/website.py", line 63, in read_secure_cookie
    cookie_val = self.request.cookies.get(name)
    AttributeError: 'NoneType' object has no attribute 'cookies'

有没有人知道为什么self.request是NoneType?

我应该补充说它是在Endpoints类中调用的:

@endpoints.api(name = 'users', version = 'v1.00',
               description = 'User Management Resources')
class UsersApi(remote.Service, MainHandler):

0 个答案:

没有答案