Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

field_class does nothing. #287

Open
dsluo opened this issue Feb 23, 2022 · 2 comments
Open

field_class does nothing. #287

dsluo opened this issue Feb 23, 2022 · 2 comments

Comments

@dsluo
Copy link

dsluo commented Feb 23, 2022

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>.

@lsmith2-edison
Copy link

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.

@gertsteyn
Copy link

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'
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants