如何获取 Django 表单 ChoiceField 中选项的标签?
我有一个 ChoiceField
,现在如何在需要时获取标签?
class ContactForm(forms.Form):
reason = forms.ChoiceField(choices=[("feature", "A feature"),
("order", "An order")],
widget=forms.RadioSelect)
form.cleaned_data["reason"]
只给我 feature
或 order
values 左右。
I have a ChoiceField
, now how do I get the label when I need it?
class ContactForm(forms.Form):
reason = forms.ChoiceField(choices=[("feature", "A feature"),
("order", "An order")],
widget=forms.RadioSelect)
form.cleaned_data["reason"]
only gives me the feature
or order
values or so.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
请参阅 Model.get_FOO_display( )。 所以,应该是这样的:
在模板中,使用如下:
See the docs on Model.get_FOO_display(). So, should be something like :
In a template, use like this:
这可能会有所帮助:
This may help:
这是最简单的方法: 模型实例引用:Model.get_FOO_display()
您可以使用此函数返回显示名称:
ObjectName.get_FieldName_display()
将
ObjectName
替换为您的类名和FieldName
以及您需要获取其显示名称的字段。This the easiest way to do this: Model instance reference: Model.get_FOO_display()
You can use this function which will return the display name:
ObjectName.get_FieldName_display()
Replace
ObjectName
with your class name andFieldName
with the field of which you need to fetch the display name of.如果表单实例已绑定,则可以使用
If the form instance is bound, you can use
这是我想出的一个方法。 可能有更简单的方法。 我使用 python manage.py shell 对其进行了测试:
注意:这可能不是很 Pythonic,但它演示了在哪里可以找到您需要的数据。
Here is a way I came up with. There may be an easier way. I tested it using
python manage.py shell
:Note: This probably isn't very Pythonic, but it demonstrates where the data you need can be found.
好的。 我知道这是非常的旧帖子,但阅读它对我帮助很大。 我想我有一些补充。
这里问题的关键在于模型方法。
不适用于表单。
如果您有一个不基于模型的表单,并且该表单有一个选择字段,那么如何获取给定选择的显示值。
这是一些可能对您有帮助的代码。
您可以使用此代码从已发布的表单中获取选择字段的显示值。
“int”的存在是基于选择的选择是一个整数。 如果选择索引是字符串,那么您只需删除 int(...)
OK. I know this is very old post, but reading it helped me a lot. And I think I have something to add.
The crux of the matter here is that the the model method.
does not work for forms.
If you have a form, that is not based on a model and that form has a choice field, how do you get the display value of a given choice.
Here is some code that might help you.
You can use this code to get the display value of a choice field from a posted form.
the 'int' is there on the basis the choice selection was a integer. If the choice index was a string then you just remove the int(...)
你可以有这样的表格:
然后这会给你你想要的:
You can have your form like this:
Then this would give you what you want:
我使用@Andrés Torres Marroquín 方式,我想分享我的实现。
结果是
这是纸
而不是纸
。希望这有帮助
Im using @Andrés Torres Marroquín way, and I want share my implementation.
And the result is
this is paper
instead ofpaper
.Hope this help
确认阿迪和保罗的反应最适合形式而不是模型。 将 Ardi 推广到任何参数:
或者将此方法放在一个单独的类中,然后在表单中将其子类化
现在为任何选择字段调用相同的方法:
confirm that Ardi's and Paul's response are best for forms and not models. Generalizing Ardi's to any parameter:
Or put this method in a separate class, and sub-class it in your form
Now call the same method for any Choice Field:
我认为也许@webjunkie 是对的。
如果您正在阅读 POST 中的表单,那么您会这样做
I think maybe @webjunkie is right.
If you're reading from the form from a POST then you would do