LOGIN_REDIRECT_URL
中添加settings.py
,然后指定url
,即使用户通过身份验证后仍尝试查看登录页面,也将对其进行重定向。 #Login settings
from django.urls import reverse_lazy
LOGIN_REDIRECT_URL = reverse_lazy('your_URL')
如果他已经通过身份验证,则以下代码将重定向并阻止他再次访问登录页面。
from django.conf import settings def loginView(request): # Below code will check if user is already authenticated then redirect if request.user.is_authenticated: return redirect(settings.LOGIN_REDIRECT_URL) # Some other code return render(request, 'loginTemplate.html')