django - 用户注销后存储会话值

时间:2011-02-13 14:33:21

标签: django django-views django-authentication django-sessions

当用户注销时,使用Django logout刷新所有会话值。 即使用户退出,我还有办法保留一些会话值吗?

1 个答案:

答案 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