1

I used CreateView for registration in my project and I want to prevent authenticated user from accessing to the register url instead redirect it to another page.

Can anyone tell me how to do it? here is my register view code:

class RegisterUserView(CreateView):
model = ChatUser
template_name = 'login/registration_page.html'
form_class = UserForm
second_form_class = ProfileForm
Razneesh
  • 1,147
  • 3
  • 13
  • 29
  • Look at this reference, https://stackoverflow.com/questions/6069070/how-to-use-permission-required-decorators-on-django-class-based-views . – JPG Sep 25 '19 at 15:19

1 Answers1

1

You can use UserPassesTestMixin for that:

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
GwynBleidD
  • 20,081
  • 5
  • 46
  • 77
  • It worked and also we can define `handle_no_permission(self)` and use `HttpResponseRedirect(reverse("redirect_url"))` for redirecting – saeed albooyeh Sep 25 '19 at 16:24