0

私の 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'

ばかげたエラーかもしれませんが、問題を解決する方法がわかりません。

前もって感謝します!

4

1 に答える 1