Skip to content

Instantly share code, notes, and snippets.

@rosehgal
Last active March 24, 2020 16:21
Show Gist options
  • Save rosehgal/23f60906982b559e424e72540a553bd5 to your computer and use it in GitHub Desktop.
Save rosehgal/23f60906982b559e424e72540a553bd5 to your computer and use it in GitHub Desktop.
from django.contrib.auth.forms import AuthenticationForm
from django.forms import ValidationError
class CustomLoginForm(AuthenticationForm):
#overriding clean method to change default authentication behaviour
def clean(self):
userid = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')
if userid and password:
#the backends will be picked from the settings from the variable named AUTHENTICATION_BACKENDS
#and the authentication method of each of one will be called in the same order as the order in the AUTHENTICATION_BACKENDS list
self.user_cache = authenticate(
userid=userid,
password=password
)
if self.user_cache is None:
raise forms.ValidationError(
self.error_messages['invalid_login'],
code='invalid_login',
params={'username': self.username_field.verbose_name},
)
else:
self.confirm_login_allowed(self.user_cache)
return self.cleaned_data
@ceragos
Copy link

ceragos commented Mar 24, 2020

from django.forms import ValidationError
line 20 raise forms.ValidationError(
You don't need the word forms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment