zostera / django-bootstrap5

Bootstrap 5 for Django

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

field_class does nothing.

dsluo opened this issue · comments

field_class is documented as an option for the bootstrap_field template tag, but it currently is not used, and does not apply the class to the <input>.

commented

This can be fixed by adding the following to the add_widget_class_attrs() function within the renderers.py file:

field_classes = [text_value(self.field_class)]

Then append "+ field_classes" to the second to last line of the function:

classes = before + classes + field_classes

This solution is working for me. If there aren't any issues with it, I think it this should be merged into the official code.

Here is a clean workaround until the bug is fixed:

custom_renderer.py

from django_bootstrap5.renderers import FieldRenderer

class CustomFieldRenderer(FieldRenderer):
    def add_widget_class_attrs(self, widget=None):
        """
        Workaround to fix bug: https://github.com/zostera/django-bootstrap5/issues/287
        """
        if widget is None:
            widget = self.widget
        super().add_widget_class_attrs(widget)
        classes = widget.attrs.get('class', '')
        if self.field_class:
            classes += f' {self.field_class}'
        widget.attrs['class'] = classes

settings.py

BOOTSTRAP5 = {
    'field_renderers': {
        'default': 'custom_renderer.CustomFieldRenderer'
    }
}