Created
October 26, 2021 14:57
-
-
Save smithdc1/65605c52f05bd39bf43fed67962b7ece to your computer and use it in GitHub Desktop.
Patch to add class level attributes for error_css_class and required_css_class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit 554a7430a2947f67e2316a6775654c66070d75a2 | |
Author: David Smith <smithdc@gmail.com> | |
Date: Tue Oct 26 15:55:57 2021 +0100 | |
Sample class attribute patch | |
diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py | |
index 5bbfcbe41c..a971407d0c 100644 | |
--- a/django/forms/boundfield.py | |
+++ b/django/forms/boundfield.py | |
@@ -170,7 +170,7 @@ class BoundField: | |
id_for_label = widget.id_for_label(id_) | |
if id_for_label: | |
attrs = {**(attrs or {}), 'for': id_for_label} | |
- if self.field.required and hasattr(self.form, 'required_css_class'): | |
+ if self.field.required and self.form.required_css_class: | |
attrs = attrs or {} | |
if 'class' in attrs: | |
attrs['class'] += ' ' + self.form.required_css_class | |
@@ -191,9 +191,9 @@ class BoundField: | |
if hasattr(extra_classes, 'split'): | |
extra_classes = extra_classes.split() | |
extra_classes = set(extra_classes or []) | |
- if self.errors and hasattr(self.form, 'error_css_class'): | |
+ if self.errors and self.form.error_css_class: | |
extra_classes.add(self.form.error_css_class) | |
- if self.field.required and hasattr(self.form, 'required_css_class'): | |
+ if self.field.required and self.form.required_css_class: | |
extra_classes.add(self.form.required_css_class) | |
return ' '.join(extra_classes) | |
diff --git a/django/forms/forms.py b/django/forms/forms.py | |
index 589b4693fd..339affc262 100644 | |
--- a/django/forms/forms.py | |
+++ b/django/forms/forms.py | |
@@ -62,6 +62,8 @@ class BaseForm(RenderableFormMixin): | |
field_order = None | |
prefix = None | |
use_required_attribute = True | |
+ error_css_class = None | |
+ required_css_class = None | |
template_name = 'django/forms/default.html' | |
template_name_p = 'django/forms/p.html' |
I like the change: both for helping PyCharm / other tooling and for showing easier upfront what is expected instead of spread in the code with hasattr
s and getattr
s.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very small changes it seems.