In our project we use django-crispy-forms and define the form layout in the form's __init__.
For the example in this repo it could look as follows (using the Bootstrap 5 template pack) to align the two fields next to each other in one row:
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Column, Row
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
Row(
Column('make'),
Column('model'),
),
)
If I want to now make the model field only appear when a make is selected by adding include=lambda form: form['make'].value() to the model field, crispy forms fails because initially the model field is not part of the form ("Could not resolve form field 'model'.").
I don't think it's a bug but I am wondering if there is a way to support this. I suspect it would require additional logic to create the form layout on the go.
In our project we use
django-crispy-formsand define the form layout in the form's__init__.For the example in this repo it could look as follows (using the Bootstrap 5 template pack) to align the two fields next to each other in one row:
If I want to now make the
modelfield only appear when a make is selected by addinginclude=lambda form: form['make'].value()to the model field, crispy forms fails because initially themodelfield is not part of the form ("Could not resolve form field 'model'.").I don't think it's a bug but I am wondering if there is a way to support this. I suspect it would require additional logic to create the form layout on the go.