Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Using AuthenticationForm in Django
- >>> from django.contrib.auth.forms import AuthenticationForm
- >>> POST = { 'username': 'test', 'password': 'me', }
- >>> form = AuthenticationForm(POST)
- >>> form.is_valid()
- False
- form = AuthenticationForm(data=request.POST)
- return self.is_bound and not bool(self.errors)
- >>> form.errors
- {'__all__': [u'Please enter a correct username and password. Note that both fields are case-sensitive.']}
- def clean(self):
- username = self.cleaned_data.get('username')
- password = self.cleaned_data.get('password')
- if username and password:
- self.user_cache = authenticate(username=username, password=password)
- if self.user_cache is None:
- raise forms.ValidationError(_("Please enter a correct username and password. Note that both fields are case-sensitive."))
- elif not self.user_cache.is_active:
- raise forms.ValidationError(_("This account is inactive."))
- self.check_for_test_cookie()
- return self.cleaned_data
Add Comment
Please, Sign In to add comment