You provide one example with HTMX, where a model field is dependant on a make field and changes it's queryset accoding to the selected make. This is simple and comprehensive.
But in real life, there are more complicated structures. e.g., depending on the selection of a field, many other fields may change (or be included or not) in the form.
While the lambda functions work and can mirror the needed behaviour of each field, the widget-tweaks' render_field templatetag can not easily fetch one form field using HTMX, it must get all of the fields.
As more interdependencies may occur in dynamic forms, it would be generally a good behaviour to get the whole form with each change and reload the form - this does not costs much and simplifies the whole workflow - in theory.
In practice, I can't wrap my head around this, I sat here for hours trying to solve it, no chance.
class UserType(IntegerChoices):
PERSON = 1
COMPANY = 2
class UserCreateForm(DynamicFormMixin, UserCreationForm):
# here we add the hx- attributes directly in the widget.
user_type = forms.ChoiceField(
choices=UserType.choices,
initial=UserType.PERSON,
widget=forms.RadioSelect(
attrs={
"hx-trigger": "change",
"hx-get": ".",
"hx-target": "#signup_form",
"hx-select": "#signup_form",
"hx-swap": "outerHTML",
"hx-params": "*",
},
),
)
While the hx-attrs get attached correctly and "work", the View is always executed without the passed GET param (e.g. ?user_type=2)
And so, the Radioselect always stays the same.
I know, this is more a question, and not a bug. Or a docs request, to add documentation for HTMX forms.
Maybe you could provide a little example on how to use HTMX for more complicated forms...? Thank you very much in advance.
BTW, I use crispy-forms too (as it's the only way of keeping code maintainable in bigger projects).
You provide one example with HTMX, where a model field is dependant on a make field and changes it's queryset accoding to the selected make. This is simple and comprehensive.
But in real life, there are more complicated structures. e.g., depending on the selection of a field, many other fields may change (or be included or not) in the form.
While the lambda functions work and can mirror the needed behaviour of each field, the widget-tweaks'
render_fieldtemplatetag can not easily fetch one form field using HTMX, it must get all of the fields.As more interdependencies may occur in dynamic forms, it would be generally a good behaviour to get the whole form with each change and reload the form - this does not costs much and simplifies the whole workflow - in theory.
In practice, I can't wrap my head around this, I sat here for hours trying to solve it, no chance.
While the hx-attrs get attached correctly and "work", the View is always executed without the passed GET param (e.g.
?user_type=2)And so, the Radioselect always stays the same.
I know, this is more a question, and not a bug. Or a docs request, to add documentation for HTMX forms.
Maybe you could provide a little example on how to use HTMX for more complicated forms...? Thank you very much in advance.
BTW, I use crispy-forms too (as it's the only way of keeping code maintainable in bigger projects).