I need to add a CSS class to the DIVs of the fields of all my forms.
The field.css_classes here seems made for this:
And, if I understand right, its implementation is here:
But I couldn’t find an easy way to add the CSS class.
I even tried overriding the template (something I would like to avoid, because it can make incompatible with future updates), creating a templates/django/forms/div.html in my app, but even after restarting the dev server it keept using the original template.
I endeded with a not so pretty patch like this (executed from my settings.py file):
from django.forms.boundfield import BoundField
if not getattr(BoundField.css_classes, 'patched', False):
unpatched = BoundField.css_classes
def css_classes(self, extra_classes=None):
return unpatched(self, ['form-field'] + (extra_classes or ))
BoundField.css_classes = css_classes
BoundField.css_classes.patched = True
Sorry - I was not attempting to answer that portion of your question. But from what I’m seeing - no. There does not appear to be a “global default” that would apply to every field being rendered. I know you can change this on a per-field basis - but then you’d need to add this to every field definition on every form, and that’s not my impression of what you’re looking for.
Side note: Actually, I would hold the opinion that overriding a template is less risky than modifying code. It’s not like there’s going to be other code dependent upon the behavior of that template.
But even that may not be necessary. As I’m looking through the source trying to find an answer, I see where BaseForm has a template_name_div attribute - if you need to do this on a by-form basis, you could set that attribute for each form individually. If you want this for every form, you could create a Form subclass that has that attribute defined, then inherit all your forms from it.