1

我正在关注 webapp2 会话文档:http ://webapp-improved.appspot.com/api/webapp2_extras/sessions.html 。

BaseHandler在这里有一个类方法check_cls_login我需要检查会话密钥“登录”是否存在。我试过self.session.get("login")但它返回错误:AttributeError: 'cached_property' object has no attribute 'get'

还定义了一个名为check_login& 它也返回错误的函数:AttributeError: 'MainHandler' object has no attribute 'session_store'

基础.py

import webapp2
from webapp2_extras import sessions

class BaseHandler(webapp2.RequestHandler):
    def dispatch(self):
       # Get a session store for this request.
       self.session_store = sessions.get_store(request=self.request)

       try:
         # Dispatch the request.
         webapp2.RequestHandler.dispatch(self)
       finally:
         # Save all sessions.
         self.session_store.save_sessions(self.response)

    @webapp2.cached_property
    def session(self):
       # Returns a session using the default cookie key.
       return self.session_store.get_session()

    def check_cls_login(self):
        # Check whether session key "login" is exist or not
        value = self.session.get("login")

def check_login(cls):
    # Check whether session key "login" is exist or not
    value = cls.session.get("login")

主文件

from Base import BaseHandler

class MainHandler(BaseHandler):
   def __init__(self, request, response):
    super(BaseHandler, self).__init__(request, response)
    BaseHandler.check_cls_login() # AttributeError: 'cached_property' object has no attribute 'get'

    self.check_cls_login() # AttributeError: 'cached_property' object has no attribute 'get'

    check_login(self) # AttributeError: 'MainHandler' object has no attribute 'session_store'
4

1 回答 1

1

而不是BaseHandler.check_login()你应该有self.check_login(). 并且 check_login 不应该是一个类方法。

于 2013-08-17T09:03:55.607 回答