Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <pre><code>class MyDatabaseSession(CallbackDict, SessionMixin): def __init__(self, initial=None, sid=None): CallbackDict.__init__(self, initial) self.sid = sid self.modified = False </code></pre> <p>The above class will now have a <strong>session id (sid) that will be stored in the cookie</strong>. 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:</p> <pre><code>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(....) </code></pre> <p>Also, you can define a model for storing session data:</p> <pre><code>class SessionData(db.Model): def __init__(self,sid,data): self.sid = sid self.data = data # and so on... </code></pre> <p>The following snippets should give you an idea:</p> <p><a href="http://flask.pocoo.org/snippets/75/" rel="nofollow noreferrer">http://flask.pocoo.org/snippets/75/</a></p> <p><a href="http://flask.pocoo.org/snippets/86/" rel="nofollow noreferrer">http://flask.pocoo.org/snippets/86/</a></p> <p><a href="http://flask.pocoo.org/snippets/110/" rel="nofollow noreferrer">http://flask.pocoo.org/snippets/110/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload