I have a class-based view that subclasses LoginView.
from django.contrib.auth.views import LoginView
class CustomLoginView(LoginView):
def get_success_url(self):
url = self.get_redirect_url()
return url or reverse_lazy('knowledgebase:user_home', kwargs={
'username':self.request.user.username,
})
I want to override the error message if a user's email is not yet active because they have to click a link sent to their email address. The current default message looks like this:
Instead of saying:
Please enter a correct email address and password. Note that both fields may be case-sensitive.
I want to say something to the effect of:
Please confirm your email so you can log in.
I tried:
accounts/forms.py
from django.contrib.auth.forms import AuthenticationForm
from django.utils.translation import gettext as _
class PickyAuthenticationForm(AuthenticationForm):
def confirm_login_allowed(self, user):
if not user.is_active:
raise forms.ValidationError(
_("Please confirm your email so you can log in."),
code='inactive',
)
accounts/views.py
class CustomLoginView(LoginView): # 1. <--- note: this is a class-based view
form_class = PickyAuthenticationForm # 2. <--- note: define form here?
def get_success_url(self):
url = self.get_redirect_url()
return url or reverse_lazy('knowledgebase:user_home', kwargs={
'username':self.request.user.username,
})
The result is absolutely no effect when I try to log in with a user that does exist, but hasn't verified their email address yet.

Method - 1
Django uses
ModelBackendas defaultAUTHENTICATION_BACKENDSand which does not authenticate the inactive users.This is also stated in Authorization for inactive users sections,
So, set
AllowAllUsersModelBackendas yourAUTHENTICATION_BACKENDSinsettings.pyHow much does it affect my Django app?
It doesn't affect anything other than the authentication. If we look into the source code of
AllowAllUsersModelBackendclass we can see it just allowing the inactive users to authenticate.Method - 2
Personally, I don't recommend this method since method-1 is the Django way of tackling this issue.
Override the
clean(...)method ofPickyAuthenticationFormclass and call theAllowAllUsersModelBackendbackend as,Result Screenshot