Guest User

Untitled

a guest
Aug 10th, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. Using AuthenticationForm in Django
  2. >>> from django.contrib.auth.forms import AuthenticationForm
  3. >>> POST = { 'username': 'test', 'password': 'me', }
  4. >>> form = AuthenticationForm(POST)
  5. >>> form.is_valid()
  6. False
  7.  
  8. form = AuthenticationForm(data=request.POST)
  9.  
  10. return self.is_bound and not bool(self.errors)
  11.  
  12.  
  13. >>> form.errors
  14. {'__all__': [u'Please enter a correct username and password. Note that both fields are case-sensitive.']}
  15.  
  16. def clean(self):
  17. username = self.cleaned_data.get('username')
  18. password = self.cleaned_data.get('password')
  19.  
  20. if username and password:
  21. self.user_cache = authenticate(username=username, password=password)
  22. if self.user_cache is None:
  23. raise forms.ValidationError(_("Please enter a correct username and password. Note that both fields are case-sensitive."))
  24. elif not self.user_cache.is_active:
  25. raise forms.ValidationError(_("This account is inactive."))
  26. self.check_for_test_cookie()
  27. return self.cleaned_data
Add Comment
Please, Sign In to add comment