Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

forms.py 1001B

123456789101112131415161718192021222324252627282930
  1. from django import forms
  2. from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm
  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 forms.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'