在 django 的 Createview 重定向经过身份验证的用户
redirect authenticated user at Createview in django
我在我的项目中使用 CreateView 进行注册,我想阻止经过身份验证的用户访问寄存器 url,而不是将其重定向到另一个页面。
谁能告诉我怎么做?
这是我的注册查看代码:
class RegisterUserView(CreateView):
model = ChatUser
template_name = 'login/registration_page.html'
form_class = UserForm
second_form_class = ProfileForm
您可以使用 UserPassesTestMixin
:
class RegisterUserView(UserPassesTestMixin, CreateView):
model = ChatUser
template_name = 'login/registration_page.html'
form_class = UserForm
second_form_class = ProfileForm
permission_denied_message = _("You are already registered!")
def test_func(self):
return self.request.user.is_anonymous
我在我的项目中使用 CreateView 进行注册,我想阻止经过身份验证的用户访问寄存器 url,而不是将其重定向到另一个页面。
谁能告诉我怎么做? 这是我的注册查看代码:
class RegisterUserView(CreateView):
model = ChatUser
template_name = 'login/registration_page.html'
form_class = UserForm
second_form_class = ProfileForm
您可以使用 UserPassesTestMixin
:
class RegisterUserView(UserPassesTestMixin, CreateView):
model = ChatUser
template_name = 'login/registration_page.html'
form_class = UserForm
second_form_class = ProfileForm
permission_denied_message = _("You are already registered!")
def test_func(self):
return self.request.user.is_anonymous