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
tomoya92
V2EX  ›  Python

webpy 开发网站,页面里可以取 session 数据吗?怎么取?

  •  1
     
  •   tomoya92 · 2017-01-06 18:04:47 +08:00 · 2301 次点击
    这是一个创建于 2672 天前的主题,其中的信息可能已经有所发展或是发生改变。

    初学 python ,在用 web.py 做 demo 练手,到 session 卡住了

    web.config.session_parameters['cookie_name'] = 'session_id'
    web.config.session_parameters['cookie_domain'] = None
    web.config.session_parameters['timeout'] = 86400,
    web.config.session_parameters['ignore_expiry'] = True
    web.config.session_parameters['ignore_change_ip'] = True
    web.config.session_parameters['secret_key'] = 'HhOduSa2Ly4uVrWg'
    web.config.session_parameters['expired_message'] = 'Session expired'
    session = web.session.Session(app, web.session.DiskStore('data/sessions'), initializer={'login': False})
    
    def session_hook():
        web.ctx.session = session
    
    app.add_processor(web.loadhook(session_hook))
    

    在页面里用 $username 取值 (username 是我保存在 session 里数据的对象),运行报错

    NameError: global name 'username' is not defined
    

    求解!

    11 条回复    2017-01-11 12:10:46 +08:00
    qwer1234asdf
        1
    qwer1234asdf  
       2017-01-06 18:08:00 +08:00
    $username?php?python?..............
    tomoya92
        2
    tomoya92  
    OP
       2017-01-06 18:10:02 +08:00
    r#1 @qwer1234asdf web.py 是 python web 开发框架
    qwer1234asdf
        3
    qwer1234asdf  
       2017-01-06 18:12:06 +08:00
    @liygheart haoba 没用过
    sobigfish
        4
    sobigfish  
       2017-01-06 18:28:54 +08:00
    tomoya92
        5
    tomoya92  
    OP
       2017-01-06 18:35:11 +08:00
    r#4 @sobigfish 这个代码我复制运行也报错

    AttributeError: 'ThreadedDict' object has no attribute 'session'

    我用的是 templates 写的页面,不是写在 python 文件里的,模板页面里的 session 数据怎么取呢?
    junnplus
        6
    junnplus  
       2017-01-06 19:29:19 +08:00
    tomoya92
        7
    tomoya92  
    OP
       2017-01-06 19:31:51 +08:00
    r#6 @junnplus 谢谢,没搜到才来问的,再次感谢 :)
    junnplus
        8
    junnplus  
       2017-01-06 19:43:17 +08:00
    @liygheart webpy template session 这三个关键词放 google 出来第一条就是
    tomoya92
        9
    tomoya92  
    OP
       2017-01-07 09:37:08 +08:00
    r#8 @junnplus 这个。。我搜的是 “ webpy 模板页面里怎么取 session 数据” 找到的都是 webpy 使用 session 的文章 尴尬了
    latyas
        10
    latyas  
       2017-01-11 00:56:35 +08:00
    web.py 是个好框架
    tomoya92
        11
    tomoya92  
    OP
       2017-01-11 12:10:46 +08:00 via Android
    @latyas 是的,我就喜欢这个
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2338 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:04 · PVG 20:04 · LAX 05:04 · JFK 08:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.