Tag Archives: django

Django 1.7 and Python 3.4 how to add a label class to your ModelForm using crispy forms

There’s a bug that’s described here: http://stackoverflow.com/questions/22901745/django-crispy-forms-bootstrap-helper-label-class-and-helper-field-class-not-wo

which can be “fixed” by changing crispy_forms/templates/bootstrap/field.html on line 8 from:

<label for="{{ field.id_for_label }}" class="control-label {% if field.field.required %}requiredField{% endif %}">

to

<label for="{{ field.id_for_label }}" class="control-label {% if field.field.required %}requiredField{% endif %}{% if label_class %} {{ label_class }}{% endif %}">

I have no idea how to get field classes working, frankly. But it is possible to get a fieldclass using django by passing it in through a widget thank goodness. Like so:


class BookForm(forms.ModelForm):
class Meta:
model = Book
exclude = ('unecessary_field')
widgets = {
'published': forms.TextInput(attrs = {'class': 'col-lg-8-or-whatever'}),
}

Took me ages to work this out. I hope it helps someone.