if user is already logged in do not show sign in form

51 Views Asked by At

I have used User Creation Form for user registration.

class UserSignUpView(SuccessMessageMixin, FormView):
    form_class = UserCreationForm
    success_url = reverse_lazy('login')
    template_name = 'core/signup.html'
    success_message = 'Account created successfully.'

When user is already logged in it should not see the signup form

1

There are 1 best solutions below

0
Mahammadhusain kadiwala On

You can achive those things using dispatch() method of class based view

from django.contrib.auth.mixins import LoginRequiredMixin

class UserSignUpView(LoginRequiredMixin, SuccessMessageMixin, FormView):
    form_class = UserCreationForm
    success_url = reverse_lazy('login')
    template_name = 'core/signup.html'
    success_message = 'Account created successfully.'

    def dispatch(self, request, *args, **kwargs):
        if self.request.user.is_authenticated:
            # User is already logged in, redirect them to another page
            return redirect('home')  # Redirect to the home page or any other desired URL
        return super().dispatch(request, *args, **kwargs)