dkoug Blog

Add bootstrap class to form fields

Django -- Posted on Aug. 17, 2018

Add bootstrap class to form fields

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class DynamicForm:
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        fields = ['CheckboxInput', 'ClearableFileInput', 'FileInput']
        for field in self.fields:
            widget_name = self.fields[field].widget.__class__.__name__
            if widget_name not in fields:
                self.fields[field].widget.attrs.update({
                    'class': 'form-control'
                })