本文介绍了django - 在用户注销后存储会话值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 Django logout
当用户注销所有会话值被刷新.即使用户注销,我也可以保留一些会话值吗?
Using the Django logout
when the user is logging out all the sessions values get flushed.I there a way to keep some of the session values even though the user logs out?
推荐答案
您可能希望使用 cookie 而不是 session 来实现这一点.
You might want to use cookie instead of session to achieve this.
# 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', '')
将上面的内容推送到模板中显示:
Push the above to the template to display:
Hello {{ remembered_username }}
参考:http://docs.djangoproject.com/en/1.2/ref/request-response/#django.http.HttpResponse.set_cookie
这篇关于django - 在用户注销后存储会话值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!