0👍
I have a similar function (designed for bootstrap 2) that gets you part of the way there; it doesn’t work with horizontal-forms like you requested, but you are able to generate dynamic rows:
from crispy_forms import helper
from crispy_forms import layout
def addBootstrapRow(helper, firstField, numFields, wrapperClasses):
# set classes used for all fields if only one provided
if type(wrapperClasses) in [str, unicode]:
wrapperClasses = [wrapperClasses]*numFields
# find first field, apply wrapper_class for each
first = helper.layout.index(firstField)
for i, cls in enumerate(wrapperClasses):
helper[first+i].wrap(layout.Field, wrapper_class=cls)
# put all these fields into a row
helper[first:first+numFields].wrap_together(layout.Div, css_class="row-fluid")
To get a format similar (but not identical) to the one in your question, you can do this:
class MyForm(ModelForm):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.helper = helper.FormHelper(self)
addBootstrapRow(self.helper, 'first_name', 2, "span6") # all fields same
addBootstrapRow(self.helper, 'username', 2, ["span4", "span8"]) # fields diff.
addBootstrapRow(self.helper, 'bio', 1, "span12")
Might not your exact solution, it may help others that stumble upon this question at least (like me).
0👍
Problem
From what I have found, there isn’t any simple solution to this problem.
In 2014, a GitHub issue regarding setting the label_class
individually for each field was created. However, this issue was closed by the crispy forms developers as out-of-scope.
In 2021, a GitHub pull request with changes allowing label_class
and field_class
on individual fields was created. However, this PR was closed due to inactivity of the PR author.
Possible solutions
The answers in this question may help you with the problem. One possible solution could be to create a custom field template – see this answer.
- [Django]-Getting Http403 when saving files through django s3 middleware (but can save using boto in shell)
- [Django]-Django CMS page_attribute falls back to other values?
- [Django]-Any clue on this error with generic relation using Django Orm?