4

logoutユーザーがログアウトしているときにDjango を使用すると、すべてのセッションの値がフラッシュされます。ユーザーがログアウトしてもセッション値の一部を保持する方法はありますか?

4

1 に答える 1

4

これを実現するには、セッションの代わりに 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

于 2011-02-14T22:07:17.367 に答える