我建議覆蓋login_required裝飾器來檢查login_state。如果login_state爲true,則正常執行其他註銷用戶會話並重定向到登錄頁面。
請按照下面的代碼。我用它來定製login_required行爲。
def custom_user_passes_test(test_func, login_url=None,
redirect_field_name=REDIRECT_FIELD_NAME):
"""
Decorator for views that checks that the user passes the given test,
redirecting to the log-in page if necessary. The test should be a callable
that takes the user object and returns True if the user passes.
"""
def decorator(view_func):
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
if test_func(request.user):
#get current login_state and store in variable named login_state
if login_state:
return view_func(request, *args, **kwargs)
else:
return redirect_to_login(path, resolved_login_url,
redirect_field_name)
return _wrapped_view
return decorator
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME,
login_url=None):
"""
Decorator for views that checks that the user is logged in, redirecting
to the log-in page if necessary.
"""
actual_decorator = custom_user_passes_test(lambda u: u.is_authenticated(),
login_url=login_url,
redirect_field_name=\
redirect_field_name)
if function:
return actual_decorator(function)
return actual_decorator
這無疑解決了自己,因爲你進入下一個頁面,用戶有一個錯誤的登錄狀態 – Sayse