Google应用引擎中的Webapp2会话 [英] Webapp2 Sessions in Google app engine

查看:141
本文介绍了Google应用引擎中的Webapp2会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚想出如何在我的Google app引擎项目中使用python实现webapp2会话。代码如下。我想知道的是最好的方法是什么?我所做的是创建了一个python文件,并将BaseHandler代码放入其中,然后我只需导入它,但我必须不断地在每个python应用程序/文件中复制配置项。

I just figured out how to implement the webapp2 sessions in my Google app engine project using python. The code is below. What I would like to know is what is the best approach to go about it? What I have done is created a python file and dropped the BaseHandler code in it then I simply import it but I have to constantly duplicate the config key in each python app/file.

BaseHandler的代码正如我从网站获得的:

The code for the BaseHandler as I got from the website:

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

要在会话中设置变量,我只需导入BaseHandler放入应用程序,并按以下示例进行调用:

To set variables in a session I simply import the BaseHandler into the app and call the following just as in the examples:

self.session['name'] = name

获取变量与示例中一样:

To get variables is just as in the example:

name = self.session.get('name')



The part I have to copy in each file is the following:

 config = {}
 config['webapp2_extras.sessions'] = {'secret_key': 'some-secret-key-to-use',}

app = webapp2.WSGIApplication([('/hello.*', MainHandler),
                            ], config=config, debug=True)

去做或者有更好的方法?谢谢

Is this the best way to go about it or are there better approaches? Thanks

推荐答案

我处理会话的方式是创建一个派生自webapp2.RequestHandler的BaseHandler类。我为所有会话初始化和默认会话值装备了BaseHandler类,并重用此类来创建每个处理程序,而不是直接从webapp2.RequestHandler派生。

The way I handled sessions was to create a BaseHandler class deriving from webapp2.RequestHandler. I equipped BaseHandler class with all session initialization and default session values and reused this class to create each handler instead of directly deriving from webapp2.RequestHandler.

这个明显的优点是您不必在每次必须在文件中使用会话时重复会话初始化。

The obvious advantage of this is that you don't have to repeat session initialization each time you have to use a session in a file.

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.
        sess = self.session_store.get_session()
        #add some default values:
        if not sess.get('theme'):
            sess['theme']='cosmo'#'slate'
        return sess


class MainPage(BaseHandler):
    def get(self):
        template = JINJA_ENVIRONMENT.get_template('stereo.html')
        if self.request.get('theme'):
            theme=self.request.get('theme')
            self.session['theme']=theme
        else:
            theme=self.session['theme']

请参阅我的文章 a>了解更多详情。

Refer my article for more details.

这篇关于Google应用引擎中的Webapp2会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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