Django: Redirect already logged user by class based view

I want to redirect to the main page if the user is already authenticated. So I want a redirect from / to /main

views.py:

class LoginView(FormView):
    form_class = LoginForm
    success_url = reverse_lazy('main')
    template_name = 'module/login.html'

    def form_valid(self, form):

        if self.request.user.is_authenticated():
            return redirect(settings.LOGIN_REDIRECT_URL)
        else:
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = authenticate(username=username, password=password)

            if user is not None and user.is_active:
                login(self.request, user)
                return super(LoginView, self).form_valid(form)
            else:
                return self.form_invalid(form)

settings.py:

LOGIN_REDIRECT_URL = reverse_lazy('main')
LOGIN_URL = reverse_lazy('login')
LOGOUT_URL = reverse_lazy('logout')

I tried also to add a get function, but then it call itself only infinite times.

def get(self, request, *args, **kwargs):

    if request.user.is_authenticated():
        return HttpResponseRedirect('main')
    else:
        return HttpResponseRedirect('/login/')

urls.py:

url(r'^$', views.LoginView.as_view(), name='login'),
url(r'^login/', views.LoginView.as_view(), name='login'),

Answer

You do redirect to the view itself in the else part, hence the infinite redirect loop. Do instead:

def get(self, request, *args, **kwargs):
    if request.user.is_authenticated():
        return HttpResponseRedirect('main')
    return super(LoginView, self).get(request, *args, **kwargs)

you should also rename the first of your two views in urls.py to 'main'.