7

我想知道auth.logout是否清除会话数据,或者我必须自己做。

from django.contrib.auth.decorators import login_required
from django.contrib import auth
@login_required
def logout(request):
    auth.logout(request)
    return redirect('base:homepage')

像这样的东西...

from django.contrib.auth.decorators import login_required
from django.contrib import auth

@login_required
def logout(request):
    for sesskey in request.session.keys():
        del request.session[sesskey]
    auth.logout(request)
    return redirect('base:homepage')

谢谢!

4

2 回答 2

18

是的。注销会刷新会话。

这是它的来源

def logout(request):
    """
    Removes the authenticated user's ID from the request and flushes their
    session data.
    """
    # Dispatch the signal before the user is logged out so the receivers have a
    # chance to find out *who* logged out.
    user = getattr(request, 'user', None)
    if hasattr(user, 'is_authenticated') and not user.is_authenticated():
        user = None
    user_logged_out.send(sender=user.__class__, request=request, user=user)

    request.session.flush()
    if hasattr(request, 'user'):
        from django.contrib.auth.models import AnonymousUser
        request.user = AnonymousUser()
于 2013-04-05T20:29:50.740 回答
0

如果您的意思是通过清除会话数据来删除“django_session”表中的记录,恐怕注销功能不会这样做。

通常,我们必须通过其他方式清除 'django_session' 表中过期的会话记录。像 crontab 任务一样定期运行“python manage.py clearsessions”

有关更多信息,请参阅此

于 2021-01-08T10:35:48.603 回答