当用户注销时,使用Django logout
刷新所有会话值。
即使用户退出,我还有办法保留一些会话值吗?
答案 0 :(得分:4)
您可能希望使用cookie而不是session来实现此目的。
# 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