web2py 将值插入会话 [英] web2py insert value into session

查看:39
本文介绍了web2py 将值插入会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了会话问题

用户从下拉菜单中选择 smth 后,我必须将该值插入会话.我需要该值才能访问模型中的 auth 表的数据库(如果我从 request.var 中读取,当我进入登录/注册表单时它会崩溃).我在哪里插入会话中的值以及如何(视图,控制器).

After a user selects smth from drop down menu I have to insert that value to session. I need that value to get to the database for auth tables in model (it crashes when I go to login/register form if I read from request.var). Where do I insert the value in session and how (view, controler).

目前我使用 cookie 解决了它,但它不是最安全的.

For now I solved it using cookies but it is not the most secure.

任何建议=

谢谢

推荐答案

session 是 Storage 类的另一个实例.任何存储到会话中的内容,例如:

session is another instance of the Storage class. Whatever is stored into session for example:

session.myvariable = "hello"

可以在以后检索:

a = session.myvariable

换句话说,它已经存在了——只需为它分配变量即可.如果你想使用数据库,你必须通过模型在你的数据库中定义一个会话表.引自 web2py 手册:

In other words, it's already there - just assign variables to it.. If you wish to use the database you have to define a session table in your DB through model. Quote from web2py manual:

例如在数据库中存储会话:

For example to store sessions in the database:

session.connect(request, response, db, masterapp=None)

其中 db 是打开的数据库连接的名称(由 DAL 返回).它告诉 web2py 您想将会话存储在数据库中而不是文件系统中.session.connect 必须在 db=DAL(...) 之后,但在任何其他需要 session 的逻辑之前,例如,设置 Auth.

where db is the name of an open database connection (as returned by the DAL). It tells web2py that you want to store the sessions in the database and not on the filesystem. session.connect must come after db=DAL(...), but before any other logic that requires session, for example, setting up Auth.

web2py 创建一个表:

web2py creates a table:

db.define_table('web2py_session',
             Field('locked', 'boolean', default=False),
             Field('client_ip'),
             Field('created_datetime', 'datetime', default=now),
             Field('modified_datetime', 'datetime'),
             Field('unique_key'),
             Field('session_data', 'text'))

并将 cPickled 会话存储在 session_data 字段中.

and stores cPickled sessions in the session_data field.

默认情况下,选项 masterapp=None 告诉 web2py 在正在运行的应用程序中尝试检索名称在 request.application 中的应用程序的现有会话.

The option masterapp=None, by default, tells web2py to try to retrieve an existing session for the application with name in request.application, in the running application.

如果您希望两个或多个应用程序共享会话,请将 masterapp 设置为主应用程序的名称.

If you want two or more applications to share sessions, set masterapp to the name of the master application.

这篇关于web2py 将值插入会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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