1
当用户注销时,使用Django logout
刷新所有会话值。 我有一种方法可以保留一些会话值,即使用户注销?django - 在用户注销后存储会话值
当用户注销时,使用Django logout
刷新所有会话值。 我有一种方法可以保留一些会话值,即使用户注销?django - 在用户注销后存储会话值
您可能想使用Cookie而不是会话来实现此目的。
# views.py, login view
# After you have authenticated a user
username = 'john.smith' # Grab this from the login form
# If you want the cookie to last even if the user closes his browser,
# set max_age to a very large value, otherwise don't use max_age.
response = render_to_response(...)
response.set_cookie('the_current_user', username, max_age=9999999999)
在你登录视图:
remembered_username = request.COOKIES.get('the_current_user', '')
按上面的模板显示:
Hello {{ remembered_username }}
参考:http://docs.djangoproject.com/en/1.2/ref/request-response/#django.http.HttpResponse.set_cookie
你说的 “保持” 是什么意思?会话是针对特定用户的,因此当用户注销时,离开他的会话是没有意义的。如果您需要在`django.contrib.auth.logout`刷新会话数据之前在db中存储一些值,您可以通过简单地覆盖`django.contrib.auth.views.logout`来完成。 – gorus 2011-02-13 17:35:40
这是什么想要做的。当用户注销时,我想将用户名保留在会话变量中,因此当他回到网站时,我可以“识别”用户,以便我可以看到“hello user”。 – avatar 2011-02-13 20:52:42