Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 如何防止用户在成功验证后进入登录页面?_Django_Python 3.x_Django Views_Django Authentication_Django Registration - Fatal编程技术网

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

Django 如何防止用户在成功验证后进入登录页面?,django,python-3.x,django-views,django-authentication,django-registration,Django,Python 3.x,Django Views,Django Authentication,Django Registration,我正在添加settings.py、root url和views.py。登录后,用户被重定向到相应的仪表板。在这种情况下,如果用户正在按back按钮或将url更改为accounts/login,那么它也应该只保留在仪表板页面上。我正在使用django注册redux 设置.py REGISTRATION_OPEN = True ACCOUNT_ACTIVATION_DAYS = 7 REGISTRATION_AUTO_LOGIN = False REGISTRATION_FORM = 'signin

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

设置.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')
url.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')),
]

您可以使用经过验证的布尔变量

然后,您需要在用户身份验证之前将其设置为
False

def registration(request):
    authenticated = False
...
然后,在用户身份验证之后,只需将var更改为
authenticated=True

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


此外,如果您需要大量使用认证,请查看自定义装饰器(),也许它们可以帮助您。

感谢您提供解决方案,但我不知道将这些条件放在何处?我正在使用django注册redux。如果您能提供更详细的信息,请提供帮助。@Panos angelopoolsplease为我们提供views.py文件的详细版本@KuntalViews.py是我上面提到的文件。用户登录后,将转到该视图。由于我使用的是django registration redux软件包,因此我没有其他用于登录的视图@Panos Angelopoulos您可以将代码片段放在您的
def用户签名(请求)
下面。此外,还需要添加user.authenticated=True。在实现此代码段之前,请在models.py文件中扩展用户模型。您可以轻松地完成此过程()。最后,使用if语句,如
if user.authenticated return redirect('dashboard'),else return redirect('login')