我是 DJango 的新手,我正在嘗試進行用戶身份驗證。我的登錄工作正常,但我的用戶沒有注銷。我的注銷視圖是:from django.contrib.auth import logoutfrom django.contrib.auth.models import Userclass LogoutView(generic.View): @staticmethod def get(request): if User.is_authenticated: # Debug statement print('if') logout(request) return redirect('login') else: return redirect('index')我的 url 工作正常,因為當我去/logout/時,我的調試語句執行但if User.is_authenticated:總是返回一個對象(真)。我該如何解決這個問題。謝謝
1 回答
翻閱古今
TA貢獻1780條經驗 獲得超5個贊
User.is_authenticated 不是你應該做的。用戶是類,顯示它將具有在您的請求中顯示的對象,這些對象已經存在,并且與正在服務的用戶無關。而,request是用戶攜帶許多東西的對象,其中之一是user。
它應該是:
request.user.is_authenticated:
添加回答
舉報
0/150
提交
取消
