Flask: 'session' vs. 'g'?

前端 未结 1 1696
-上瘾入骨i
-上瘾入骨i 2020-12-13 01:56

I\'m trying to understand the differences in functionality and purpose between g and session. Both are objects to \'hang\' session data on, am I ri

相关标签:
1条回答
  • 2020-12-13 02:05

    No, g is not an object to hang session data on. g data is not persisted between requests.

    session gives you a place to store data per specific browser. As a user of your Flask app, using a specific browser, returns for more requests, the session data is carried over across those requests.

    g on the other hand is data shared between different parts of your code base within one request cycle. g can be set up during before_request hooks, is still available during the teardown_request phase and once the request is done and sent out to the client, g is cleared.

    0 讨论(0)
提交回复
热议问题