小编典典

如何在Django表单ChoiceField中获取选择的标签?

python

我有一个ChoiceField,现在如何在需要时获取“标签”?

class ContactForm(forms.Form):
     reason = forms.ChoiceField(choices=[("feature", "A feature"),
                                         ("order", "An order")],
                                widget=forms.RadioSelect)

form.cleaned_data["reason"] 只会给我“功能”或“命令”左右。


阅读 274

收藏
2021-01-20

共1个答案

小编典典

这可能会有所帮助:

reason = form.cleaned_data['reason']
reason = dict(form.fields['reason'].choices)[reason]
2021-01-20