GoodCloud / django-zebra

Forms, widgets, template tags and examples that make Stripe + Django easier.
MIT License
194 stars 68 forks source link

Allow css classes to come from self.attrs without being overwritten. #39

Open amjoconn opened 10 years ago

amjoconn commented 10 years ago

I am using a bootstrap form mixin which includes code similar to this

class BootstrapForm(object):
    def __init__(self, *args, **kwargs):
        super(BootstrapForm, self).__init__(*args, **kwargs)
        for bfield in self:
            field = self.fields[bfield.name]
            classes = ['form-control']
            if 'class' in field.widget.attrs:
                classes.append(field.widget.attrs['class'])
            field.widget.attrs.update(
                {'class' : ' '.join(classes)}
            )

It depends on the value of field.widget.attrs['class'] being propagated to the rendered widget.

One could argue that Django should be doing the combining for us, but it doesn't, for now this works, and I don't believe will cause any other issues.