Skip to content

Support for crispy forms with custom layouts #4

@mschoettle

Description

@mschoettle

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions