我想将表单拆分为两个具有各自 css_id 的 div,以便我可以根据第一个 div 中的选择使用 ajax 更新第二个 div。问题是,我生成的表单没有 id 为“pbid”和“pb-options”的 div
class PBForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.layout = Layout(
Div('cc', 'pb_id', css_id="pbid"),
Div('closed_r', 'closed_l', css_id="pb-options")
)
pb_id = forms.ChoiceField(label='PB', widget=forms.RadioSelect)
closed_l = forms.ChoiceField(label='Left Closed', widget=forms.RadioSelect, choices=BOOL_CHOICES)
closed_r = forms.ChoiceField(label='Right Closed', widget=forms.RadioSelect, choices=BOOL_CHOICES)
class Meta:
model = pb
fields = ['cc', 'pb_id', 'closed_r', 'closed_l']
您将其分配
.layout
给FormHelper
: