forms.py 1023 B

12345678910111213141516171819202122232425262728293031
  1. from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm
  2. from django.core.exceptions import ValidationError
  3. from django.utils.translation import gettext_lazy as _
  4. class AdminAuthenticationForm(AuthenticationForm):
  5. """
  6. A custom authentication form used in the admin app.
  7. """
  8. error_messages = {
  9. **AuthenticationForm.error_messages,
  10. "invalid_login": _(
  11. "Please enter the correct %(username)s and password for a staff "
  12. "account. Note that both fields may be case-sensitive."
  13. ),
  14. }
  15. required_css_class = "required"
  16. def confirm_login_allowed(self, user):
  17. super().confirm_login_allowed(user)
  18. if not user.is_staff:
  19. raise ValidationError(
  20. self.error_messages["invalid_login"],
  21. code="invalid_login",
  22. params={"username": self.username_field.verbose_name},
  23. )
  24. class AdminPasswordChangeForm(PasswordChangeForm):
  25. required_css_class = "required"