Flask - 像使用Cookie一样将会话数据保存在数据库中 [英] Flask - Save session data in database like using cookies

查看:298
本文介绍了Flask - 像使用Cookie一样将会话数据保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask创建一个网络应用程序。



我不知道是否可以保存用户会话数据

  session ['ishappy'] = true 

在数据库中,就像在Django中使用SessionMiddleware一样,您可以在Cookie和数据库之间进行选择。



如果是我应该导入到Flask应用程序。 p>

解决方案

我建议你通过子类化烧瓶默认值实现自己的Session和SessionInterface。基本上,你需要定义自己的会话类和会话接口类。

  class MyDatabaseSession(CallbackDict,SessionMixin):

def __init __(self,initial = None,sid = None):
CallbackDict .__ init __(self,initial)
self.sid = sid
self.modified = False

上述类现在将有一个会话ID cookie 。所有与此会话ID相关的数据将存储在您的mysql数据库中。为此,你需要实现下面的类和方法:

  class MyDatabaseSessionInterface(SessionInterface):

def __init __(self,db):
#这可能是你的mysql数据库或sqlalchemy db对象
self.db = db

def open_session(self,app,request ):
#查询您的cookie的会话ID
sid = request.cookies.get(app.session_cookie_name)

如果sid:
#现在您查询数据库中的会话数据
#最后你将返回一个MyDatabaseSession对象

def save_session(self,app,session,response):
#保存sesion数据
#返回一个包含详细信息的响应cookie
response.set_cookie(....)

此外,您还可以定义存储会话数据的模型:

  class SessionData(db.Model):
def __init __(self,sid,data):
self.sid = sid
self.data = data
#等等...



以下代码段应该给你一个想法:



http://flask.pocoo.org/snippets/75/



http://flask.pocoo.org/snippets/86/



http://flask.pocoo.org/snippets/110 /


I am creating a web app using Flask.

I wonder if it is possible to save user session data like

session['ishappy'] = true

in database like it's done in Django using SessionMiddleware where you have options to choose between cookies and database.

And if it is what should I import to my Flask app.

解决方案

I suggest you implement your own Session and SessionInterface by subclassing flask defaults. Basically, you need to define your own session class and a session interface class.

class MyDatabaseSession(CallbackDict, SessionMixin):

    def __init__(self, initial=None, sid=None):
    CallbackDict.__init__(self, initial)
    self.sid = sid
    self.modified = False

The above class will now have a session id (sid) that will be stored in the cookie. All the data related to this session id will be stored in your mysql database. For that, you need to implement the following class and methods below:

class MyDatabaseSessionInterface(SessionInterface):

    def __init__(self, db):
        # this could be your mysql database or sqlalchemy db object
        self.db = db

def open_session(self, app, request):
    # query your cookie for the session id
    sid = request.cookies.get(app.session_cookie_name)

    if sid:
       # Now you query the session data in your database
     # finally you will return a MyDatabaseSession object

def save_session(self, app, session, response):
    # save the sesion data if exists in db
    # return a response cookie with details
     response.set_cookie(....) 

Also, you can define a model for storing session data:

class SessionData(db.Model):
    def __init__(self,sid,data):
        self.sid = sid
        self.data = data
   # and so on...

The following snippets should give you an idea:

http://flask.pocoo.org/snippets/75/

http://flask.pocoo.org/snippets/86/

http://flask.pocoo.org/snippets/110/

这篇关于Flask - 像使用Cookie一样将会话数据保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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