[Fixed]-How to overwrite the confirm_login_allowed function in the Django AuthenticationForm class?

1๐Ÿ‘

โœ…

I recommend you read the login source code:
https://docs.djangoproject.com/en/1.8/_modules/django/contrib/auth/views/

Solution:
You need to create a form that is a subclass of AuthenticationForm in django.contrib.auth.forms and add overwrite the confirm_login_allowed in there such as below:

in forms.py:

class CustomAuthenticationForm(AuthenticationForm):
    def confirm_login_allowed(self, user):
        if not user.is_active:
            raise forms.ValidationError(
                _("Your account has expired. \
                Please click the renew subscription link below"),
                code='inactive',
            )

Update your urls.py file โ€“ update the kwargs for the login view:

url(r'^login/$', login, {'authentication_form':forms.CustomAuthenticationForm}, name='user_login'),
๐Ÿ‘คnael

Leave a comment