如何防止用户在成功验证后进入登录页面?

how can I prevent user to go to login page after successful authentication?

我正在添加 settings.py、根目录 url 和 views.py。登录用户被重定向到相应的仪表板后。在这种情况下,如果用户按下后退按钮或将 url 更改为 accounts/login,那么它也应该只保留在仪表板页面上。我正在使用 django-registration-redux

settings.py

REGISTRATION_OPEN = True
ACCOUNT_ACTIVATION_DAYS = 7
REGISTRATION_AUTO_LOGIN = False
REGISTRATION_FORM = 'signin.forms.MyRegForm'
LOGIN_REDIRECT_URL = '/signin/user_sign/'

views.py

def user_sign(request):
    obj = UserSelection.objects.get(user=request.user)

    if obj.user_type == 'candidate':
        return redirect('candidate:cand_dash')

    else:
        return redirect('employer:employer_dash')

urls.py

from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from signin.regbackend import MyRegistrationView
from django.contrib.auth import views as auth_views

urlpatterns = [
    url(r'^$', auth_views.LoginView.as_view(template_name='registration/login.html'), name='home'),
    url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'),
    url(r'^accounts/', include('registration.backends.default.urls')),

    url(r'^candidate/', include('candidate.urls')),
    url(r'^employer/', include('employer.urls')),
    url(r'^signin/', include('signin.urls')),
]

您可以使用布尔变量 authenticated

那么在用户认证前需要设置为False

def registration(request):
    authenticated = False
...

然后在用户身份验证后将 var 更改为 authenticated = True

最后,每次您需要知道用户是否已通过身份验证时,只需使用 if user.authenticated

此外,如果您需要大量使用经过身份验证的功能,请查看自定义装饰器 (https://docs.djangoproject.com/en/2.0/topics/http/decorators/),也许它们可以帮助您。