Django - 表单小部件 TypeChoiceField - 如何设置两个单选按钮的 id
我正在使用带有 TypeChoiceField 的表单,这是表单的代码:
class AnagraficaForm(forms.Form):
usertype = ((1,'Privato'),(0,'Libero professionista/Azienda'))
nome = forms.CharField(max_length=100)
cognome = forms.CharField(max_length=100)
telefono = forms.CharField(max_length=50,required=False)
email= forms.EmailField(max_length=100,required=False)
indirizzo = forms.CharField(max_length=100)
cap = ITZipCodeField(required=False)
citta = forms.CharField(max_length=100)
codfisc = ITSocialSecurityNumberField(required=False)
piva = ITVatNumberField(required=False)
ragsociale = forms.CharField(max_length=100)
is_privato = forms.TypedChoiceField(
initial=1,
coerce=lambda x: bool(int(x)),
choices=usertype,
#using custom renderer to display radio buttons on the same line
widget=forms.RadioSelect(renderer=HorizRadioRenderer)
)
现在我正在尝试为显示的两个单选按钮设置自定义 id,但我还没有找到正确的方法。有什么想法吗?
谢谢-卢克
i'm using a form with a TypeChoiceField, this is the form's code:
class AnagraficaForm(forms.Form):
usertype = ((1,'Privato'),(0,'Libero professionista/Azienda'))
nome = forms.CharField(max_length=100)
cognome = forms.CharField(max_length=100)
telefono = forms.CharField(max_length=50,required=False)
email= forms.EmailField(max_length=100,required=False)
indirizzo = forms.CharField(max_length=100)
cap = ITZipCodeField(required=False)
citta = forms.CharField(max_length=100)
codfisc = ITSocialSecurityNumberField(required=False)
piva = ITVatNumberField(required=False)
ragsociale = forms.CharField(max_length=100)
is_privato = forms.TypedChoiceField(
initial=1,
coerce=lambda x: bool(int(x)),
choices=usertype,
#using custom renderer to display radio buttons on the same line
widget=forms.RadioSelect(renderer=HorizRadioRenderer)
)
now i'm trying to set a custom id for the two radio buttons displayed but i havent found yet the right way. any idea?
thx - Luke
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要自定义渲染器
HorizRadioRenderer
的render
方法,因为您需要为每组单选按钮自定义它。如果它只是一个字段,您可以使用 attrs 参数来自定义 id。You need to customize the renderer
HorizRadioRenderer
'srender
method, as you need to customize it for each set of radio buttons. If it was just a single field, you could have used the attrs argument to customize the id.