Question

i want to prevent/redirect logged in users from viewing login or signup pages, i did that through views like this

def login(request):
    if request.user.is_authenticated():
        #redirect
    else:
        #view form`

but now i switch to generic view

class LoginView(generic.edit.FormView):
    form_class = LoginForm
    template_name = "people/user_login_form.html"
    success_url = '/'

and couldn't use the variable 'request', after some searching i found a suggestion to use a custom_login function but i didn't know how to complete it

def custom_login(request, **kwargs):
    if request.user.is_authenticated():
        return redirect('/')
    else:
        #what should i implement here to render LoginView.as_view() ?

is there a better way ? permissions maybe ?

Was it helpful?

Solution

I would override dispatch method:

class LoginView(generic.edit.FormView):
    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated():
            return HttpRedirect('/whatever/')
        else:
            return super(LoginView, self).dispatch(request, *args, **kwargs)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top