I am using crispy_forms and FormHelper. I have a model field declared as:
active = models.BooleanField(default=True)
And in my ModelForm, I have tried both the following in my Layout:
self.helper.layout = Layout(
...
InlineCheckboxes('active'),
Field('active'),
...
which both not providing the desired result:
Please see image link
While using InlineCheckboxes, I do not see the checkbox and using only Field, it's not formatted correctly.
Please help
Here is the link to the "Bootstrap Layout objects" section of Crispy Forms docs.
InlineCheckboxes isn't appropriate for your model's field-type.
A hacky way to achieve what you're looking for is to use
PrependedText
with an empty string for thetext
argument.Examining the source it appears that a boolean field by default renders the
<input>
tag inside the<label>
tag. Using the hack above, 'Active' stays in the<label>
and the<input>
is put where you'd expect: in a<div>
with "control" css class. Compare the following:PrependedText('active', '')
:Field('active')
:Update
I've confirmed that this is fixed in the dev branch of django-crispy-forms.
Reference this commit: 5c3a268
And this github issue: #267