[Fixed]-How to login a user using forms?

1👍

The login function takes request as first parameter. So you have write confirm_login_allowed like this:

def confirm_login_allowed(self, request):
        if self.user_cache.is_active:
            login(request,self.user_cache)   
        else:
            raise forms.ValidationError(
                self.error_messages['inactive'],
                code='inactive',
                )

And call it in the view like this

def user_login(request):
    if request.method == 'POST':

        form = AuthenticationForm(request.POST)

        if form.is_valid():
             form.confirm_login_allowed(request)
             return HttpResponseRedirect(reverse('student:mains'))
        else:
             ....

Also don’t call the confirm_login_allowed method from clean method because it does’t have the request.

👤ruddra

Leave a comment