-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for crispy forms with custom layouts #4
Comments
Sorry for the slow reply on this. I don't use |
Hi! I use both crispy and this package. I don’t (currently) change the layout, but what I have done is set the Model field to be hidden if the Make is blank. Not sure if that’s a suitable workaround |
You could change the DynamicFormMixin code to the following: ...
else:
self.fields[name].widget = forms.HiddenInput()
#del self.fields[name] Hiding a field is better, as it still includes it in the form, and CrispyForms or the validation won't complain. |
You could add an attribute to DynamicFormMixin (like Even better would be to make the "including" configureable on a per-field basis: the "include" parameter should hide the field, and a new, "disabled" parameter could dynamically disable the field, depending on the lambda outcome. This would mask the "disabled" parameter of the read field, and would make it dynamic! |
This is even more complicated, as hiding the field does a good job when just using django-forms-dynamic. But when you are using e.g. HTMX to dynamically change field querysets in the frontend together with crispy forms, it gets messy, as crispy forms adds a wrapper div which contains the label. And hiding just the e.g. input field does not hide the label. |
I managed to solve the problem by reloading the whole form using HTMX, and class PrepopulateFormViewMixin:
"""Prepopulates the form with values from GET parameters."""
def get_initial(self):
initial = super().get_initial()
initial.update(clean_dict(self.request.GET.dict()))
return initial |
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:
If I want to now make the
model
field only appear when a make is selected by addinginclude=lambda form: form['make'].value()
to the model field, crispy forms fails because initially themodel
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.
The text was updated successfully, but these errors were encountered: