在我的 GAE webapp2 应用程序中,我将处理程序拆分为不同的文件。
所以我有basehandler.py,它包含:
import webapp2
from webapp2_extras import sessions
class BaseHandler(webapp2.RequestHandler):
def dispatch(self):
# Get a session store for this request
print "dispatching........"
self.session_store = sessions.get_store(request = self.request)
try:
webapp2.RequestHandler.dispatch(self)
finally:
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()
我从 main.py 导入这个,
import os
import urllib
import webapp2
import jinja2
from basehandler import BaseHandler
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'])
class MainPage(BaseHandler):
def get(self):
self.session['user'] = 'logged'
# Define values that will be passed onto the template
template_values = {
'title': 'TITLE'
}
# search for an appropriate template and render it.
template = JINJA_ENVIRONMENT.get_template('./templates/index.html')
self.response.write(template.render(template_values))
config = {}
config['webapp2_extras.sessions'] = {
'secret_key': 'my-super-secret-key'
}
application = webapp2.WSGIApplication([
('/', MainPage)
], config = config, debug = True)
但是,当我启动应用程序时,出现以下错误:
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine- default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine- default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine- default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine- default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/Users/sewonjang/Developer/GAE/Tackl/basehandler.py", line 17, in dispatch
webapp2.RequestHandler.dispatch(self)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine- default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine- default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/Users/sewonjang/Developer/GAE/Tackl/main.py", line 19, in get
self.session['user'] = 'logged'
AttributeError: 'MainPage' object has no attribute 'session'
这可能是一个愚蠢的错误,但我不知道如何解决这个问题。
先感谢您!