在整个登录期间保留会话变量 [英] Persisting session variables across login

查看:70
本文介绍了在整个登录期间保留会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在会话变量中保存有关用户首选项的信息。如果用户在注销时选择一个首选项,然后再登录,则我希望保持该首选项而无需重新选择它。

I want to hold information about a users preferences in a session variable. If the user chooses a preference while logged out and then later logs in, I want the preference to be maintained without needing to reselect it.

Django会话在cookie中维护会话密钥,以跟踪用户会话。据我了解,该键在用户登录时会更改。

Django sessions maintain a session key in a cookie to track a users session. The way I understand it, this key is changed when a user logs in.

a)这是否意味着登录时删除了所有会话变量,或者是否存在任何逾越节

a) Does this mean all session variables are deleted on login or is there any sort of passover

b)在无法保存登录名的情况下,手动设置cookie是最好的方法吗?我想像这样的情况:

b) In the case of not being able to save preferences across a login, is manually setting cookies the best way to proceed? I imagine a scenario like:


  • 注销时,在登录时保持cookie中的首选项

  • ,将首选项复制到会话变量并通过登录写入db(通过信号?)

  • 注销时,使用首选项更新cookie(通过信号?)

更新

我也通过在用户的配置文件对象中保存首选项来获得此功能就像Cookie中一样(这些首选项无论如何都不敏感)。用户登录后,其个人资料设置将具有优先级。未登录时,将选择cookie首选项

I managed to get this functionality by saving preferences in the User's profile object, as well as in a cookie (these preferences are not sensitive in any way). When the user is logged in, their profile setting takes preference. When not logged in, the cookie preference is chosen

推荐答案

登录/注销时,Django将刷新全部 >会话(如果另一个用户登录)(auth / init .py中的request.session.flush())。

When you login/logout Django will flush all sessions if another user logs in (request.session.flush() in auth/init.py).

最好将用户设置存储在数据库中,并添加一些中间件来获取数据并将其存储在请求中。

You're better of storing user settings in the database and add some middleware to get that data and store it in your request.

这篇关于在整个登录期间保留会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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