将basehandler放到不同的文件时,GAE webapp2会话处理不起作用 [英] GAE webapp2 session handling not working when putting basehandler to a different file

查看:178
本文介绍了将basehandler放到不同的文件时,GAE webapp2会话处理不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的GAE webapp2应用程序中,我将处理程序分成不同的文件。



所以我有basehandler.py,它包含:

  import webapp2 
从webapp2_extras导入会话
$ b $ class BaseHandler(webapp2.RequestHandler):
def dispatch(self):
#获取此请求的会话存储
打印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):
#使用默认的cookie关键字返回一个会话
返回self.session_store.get_session()

我从main.py导入这个文件,

  import os 
import urllib
import webapp2
从basehandler导入jinja2
导入BaseHandler


JINJA_ENVIRONMENT = jinja2.Environment(
loader = j (b)b
$ b class MainPage(BaseHandler):
def get() (self):

self.session ['user'] ='logged'

#定义传递给模板的值
template_values = {
'title':'TITLE'
}

#搜索合适的模板并进行渲染。
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(最近的最后调用):
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine- default.bundle / Contents / Resources / google_appengine / lib / webapp2-2.5.2 / webapp2.py,第1535行,在__call__
rv = self.handle_exception(request,response,e)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine- default.bundle / Contents / Resources / google_appengine / lib / webapp2- 2.5.2 / webapp2.py,lin e 1529,在__call__
rv = self.router.dispatch(request,response)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine- default.bundle / Contents / Resources / google_appengine / lib / webapp2-2.5.2 / webapp2.py,第1278行,在default_dispatcher
中返回route.handler_adapter(请求,响应)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine- default.bundle / Contents / Resources / google_appengine / lib / webapp2-2.5.2 / webapp2.py,第1102行,在__call__
return handler.dispatch()
文件/ Users / sewonjang / Developer /GAE/Tackl/basehandler.py,第17行,发送
webapp2.RequestHandler.dispatch(self)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine- default.bundle /目录/资源/ google_appengine / lib / webapp2-2.5.2 / webapp2.py,第572行,发送
返回self.handle_exception(e,self.app.debug)
文件/ Applic ations / GoogleAppEngineLauncher.app / Contents / Resources / GoogleAppEngine- default.bundle / Contents / Resources / google_appengine / lib / webapp2-2.5.2 / webapp2.py,第570行,发送
返回方法(* args, ** kwargs)
文件/Users/sewonjang/Developer/GAE/Tackl/main.py,第19行,获取
self.session ['user'] ='logged'
AttributeError:'MainPage'对象没有属性'session'

这可能是一个愚蠢的错误,但我无法弄清楚如何解决这个问题。



提前致谢!

解决方案

这是一个愚蠢的问题。



这是BaseHandler中的一个简单的缩进错误


In my GAE webapp2 application, I split up my handlers in different files.

So I have basehandler.py, which holds:

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()

And I'm importing this from 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)

However, when I lauch the application, I get the following error:

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'

It might be a silly error, but I can't figure out how to solve the problem.

Thank You in advance!

解决方案

It was a silly question.

It's a simple indentation error in BaseHandler

这篇关于将basehandler放到不同的文件时,GAE webapp2会话处理不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆