V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
shinerene
V2EX  ›  Python

Flask 中的 Session 和 SessionInterface 的问题

  •  
  •   shinerene ·
    Xiechen · 2014-06-01 07:02:46 +08:00 · 4249 次点击
    这是一个创建于 3618 天前的主题,其中的信息可能已经有所发展或是发生改变。
    对于Flask中的Session和SessionInterface该如何使用?有没有可以提供一点实例代码?
    比如说初始化session 和sessioninterface的open_session和save_session该怎么样使用?
    有没有实例代码?
    7 条回复    2014-06-03 09:39:01 +08:00
    wuxqing
        1
    wuxqing  
       2014-06-01 08:36:27 +08:00
    shinerene
        2
    shinerene  
    OP
       2014-06-01 16:24:31 +08:00
    恩 我知道这个 但是应该怎么在主代码中使用这个interface和session呢?能说一下实际该怎么应用么
    dagger
        3
    dagger  
       2014-06-01 19:49:51 +08:00
    初始化之后不用再管SessionInterface了吧,session使用的话,教程里就有啊
    http://flask.pocoo.org/docs/quickstart/#sessions
    shinerene
        4
    shinerene  
    OP
       2014-06-01 23:32:56 +08:00
    @dagger 可是我用help()看了session,Session是不一样的,Session是SecureCookieSession类,而session是定义在global类中的,为localproxy()。这两个我在应用的时候应该用Session吧
    dagger
        5
    dagger  
       2014-06-02 09:34:26 +08:00
    flask通过特殊手段让一些全局变量来代理当前上下文的相关变量:
    http://flask.pocoo.org/docs/appcontext/
    http://werkzeug.pocoo.org/docs/local/

    原理细节你可以自己去探究,不过使用的时候你连教程都不相信,那也没办法了。。
    shinerene
        6
    shinerene  
    OP
       2014-06-02 19:10:31 +08:00
    @dagger 额 谢谢了 我还是相信教程的 只不过 我自己重新写了一个server-side的session 所以我不知道该如何使用它
    dagger
        7
    dagger  
       2014-06-03 09:39:01 +08:00
    用起来一样的,flask的模块化设计非常好,默认的session也只不过是SessionMixin、SessionInterface的一个实现罢了(SecureCookieSession、SecureCookieSessionInterface)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2799 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 03:58 · PVG 11:58 · LAX 20:58 · JFK 23:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.