K
K
kpronink2017-09-28 09:02:07
Django
kpronink, 2017-09-28 09:02:07

How to check in Base Views that the user is already logged in and not show him the login page?

class LoginFormView(FormView):

    form_class = AuthenticationForm

    template_name = "login.html"

    success_url = "/"

    def form_valid(self, form):
        self.user = form.get_user()

        login(self.request, self.user)
        return super(LoginFormView, self).form_valid(form)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pan Propan, 2017-09-28
@mgis

https://docs.djangoproject.com/en/1.11/ref/contrib...

S
shudry_toster, 2017-09-28
@shudry_toster

from django.urls import reverse
from django.shortcuts import redirect

class AlreadyLoggedIn(object):
    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated:
            return redirect(reverse('your-url'))
        else:
            return super(AlreadyLoggedIn, self).dispatch(request, *args, **kwargs)

And now it only remains to add:
class LoginFormView(AlreadyLoggedIn, FormView):
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question