Django/Auth:注销是否清除会话数据?

9

我想知道auth.logout是否会清除session数据,还是需要我自己清除。

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')

Something like this...

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')

谢谢!


2个回答

21

是的。注销会刷新会话。

这是它的源代码

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()

0

如果您的意思是通过清除会话数据来删除'django_session'表中的记录,我恐怕注销功能并不能实现这一点。

通常,我们需要通过其他方式清除'django_session'表中已过期的会话记录。例如使用crontab任务定期运行'python manage.py clearsessions'

请查看此处了解更多信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接