gpt4 book ai didi

Django/Auth : logout clears the session data?

转载 作者:行者123 更新时间:2023-12-04 13:23:32 24 4
gpt4 key购买 nike

我想知道是否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')

像这样的东西...
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')

谢谢!

最佳答案

是的。注销会刷新 session 。

这是 its source :

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

关于Django/Auth : logout clears the session data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15842905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com