python - 如何防止登录用户访问 Django 1.9 版本中的 'Log In' 或 'Sign Up' 页面?

标签 python django

我对 Python 还很陌生。

我的问题是我想限制已登录的用户访问登录和注册页面。

本质上,我正在寻找类似 @​​login_required 装饰器的东西,它将允许登录的用户访问这些页面。

到目前为止,我已经

这是我用于登录的views.py:

def login_view(request): # Login View to begin user session
print(request.user.is_authenticated())
if request.method == 'POST':
    form = UserLogInForm(request.POST)
    if form.is_valid():
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        user = authenticate(username=username, password=password)
        if user is not None:
                login(request, user)
                print(request.user.is_authenticated())
                return HttpResponseRedirect('/')
        else:
            form = UserLogInForm()
        return render(request, 'login.html', {'form': form})
else:
    form = UserLogInForm()
return render(request, 'login.html', {'form': form})

然后注册:

class signup_view(View):
    form_class = UserSignUpForm
    template_name = 'signup.html'
    # Blank form is requested
    def get(self, request):
        form = self.form_class(None)
        return render(request, self.template_name, {'form': form})
        # Form is filled in and posted to DB - Process the Data
    def post(self, request):
        form = self.form_class(request.POST)
        if form.is_valid():
            # Further checks before committing object to the DB
            # Cleaned Data
            first_name = form.cleaned_data['first_name']
            last_name = form.cleaned_data['last_name']
            email = form.cleaned_data['email']
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = User.objects.create_user(first_name=first_name,
                                            last_name=last_name,
                                            email=email,
                                            username=username,
                                            password=password
             )
             user.set_password(password)
             user.save()

             user = authenticate(email=email, username=username, password=password)
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return HttpResponseRedirect('/')

    return render(request, self.template_name, {'form': form})

如果有人知道任何解决方案,那就太棒了,谢谢。

最佳答案

由于您使用的是基于类的 View ,因此可以使用 UserPassesTestMixin并在检查方法中测试用户是匿名的:

class signup_view(UserPassesTestMixin, View):
    ...
    def test_func(self):
        return self.request.user.is_anonymous()

您可以使用 @user_passes_test 装饰器对另一个 View 执行类似的操作。

关于python - 如何防止登录用户访问 Django 1.9 版本中的 'Log In' 或 'Sign Up' 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39415019/

相关文章:

python - 如何避免循环依赖?

Python-属性错误: Point instance has no attribute 'x'

python - Django fixtures 以默认值保存

javascript - Django 民意调查教程第 3 部分 : polls/templates/polls/index. html - 此代码是什么?这不是 python

python - Django:数据透视表

django - get_or_create 对于小数的 unique_together 失败

python - 我怎样才能找到平均水平最好的州

具有多个内连接的 Django ORM 查询

python - 从 render.com 磁盘检索 Django 媒体文件

Python - 在使用正则表达式查找句点(句号或 .)时,我应该使用字符串前缀 r 吗?