forms.py 855 B

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