限制经过身份验证的用户登录页面的最佳方法是什么

What is the best way to restrict login page for authenticated users

我有以下中间件:

class SimpleMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        self.login_path = '/login/'
        self.home_path = '/'

    def __call__(self, request):

        if request.user.is_authenticated and request.path == self.login_path:
            return redirect(self.home_path)

        response = self.get_response(request)
        return response

如果任何已经授权的用户尝试访问登录页面,将他们重定向到主页是个好主意吗?提前致谢!

登录视图中有一个 redirect_authenticated_user 属性。

from django.contrib.auth.views import LoginView

class CustomLoginView(LoginView):
    redirect_authenticated_user = True