forms.py 825 B

12345678910111213141516171819202122232425
  1. from django.contrib.admin.forms import AdminAuthenticationForm, AdminPasswordChangeForm
  2. from django.contrib.admin.helpers import ActionForm
  3. from django.core.exceptions import ValidationError
  4. class CustomAdminAuthenticationForm(AdminAuthenticationForm):
  5. class Media:
  6. css = {"all": ("path/to/media.css",)}
  7. def clean_username(self):
  8. username = self.cleaned_data.get("username")
  9. if username == "customform":
  10. raise ValidationError("custom form error")
  11. return username
  12. class CustomAdminPasswordChangeForm(AdminPasswordChangeForm):
  13. def __init__(self, *args, **kwargs):
  14. super().__init__(*args, **kwargs)
  15. self.fields["old_password"].label = "Custom old password label"
  16. class MediaActionForm(ActionForm):
  17. class Media:
  18. js = ["path/to/media.js"]