2

I am extending Django's AuthenticationForm in order to provide better error messaging to the user. If login is going to fail, I want to perform an additional check on the user's input to provide a less generic message explaining to the user why login failed.

urls:py:

urlpatterns = patterns(
   'django.contrib.auth.views',
    url(r'^login/$', 'login', {'authentication_form': RemoteLoginAwareLoginForm}, name='auth_login'),
)

forms.py:

class RemoteLoginAwareLoginForm(AuthenticationForm):
    def clean(self):
      cleaned_data = super(AuthenticationForm, self).clean()
      if self.errors:  # <==== self.errors is {}, even when login will fail
          # Perform additional checks
      return cleaned_data

I thought that the right way to do this was to override clean() as above, but as indicated, self.errors is empty even when I logged in with bad credentials. I tried to override full_clean() and validate() as well, but neither got called at all.

Update:

I realize that the error I'm looking for -- that the credentials aren't valid -- doesn't come until the code actually attempts to log the user in. I added a custom ModelBackend where I am able to catch the error... but I am not sure how to report it back up as there is no request here.

Is the answer that this requires a custom login view?

Thanks!

1 Answer 1

0

There is a similar problem on stackoverflow. But I'm not sure if it solves your problem.

Using AuthenticationForm in Django

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.