如何在Django表单中获得一个选择的标签?

时间:2021-02-16 19:21:23

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"] would only give me "feature" or "order" or so.

的形式。cleaned_data["reason"]将只提供“特性”或“顺序”之类的内容。

7 个解决方案

#1


58  

This may help:

这可以帮助:

reason = form.cleaned_data['reason']
reason = dict(form.fields['reason'].choices)[reason]

#2


105  

See the docs on Model.get_FOO_display(). So, should be something like :

请参阅Model.get_FOO_display()中的文档。所以,应该是:

ContactForm.get_reason_display()

In a template, use like this:

在模板中,如下所示:

{{ OBJNAME.get_FIELDNAME_display }}

#3


14  

This the easiest way to do this: Model instance reference: Model.get_FOO_display()

最简单的方法是:模型实例引用:Model. get_foo_display ()

You can use this function which will return the display name: ObjectName.get_FieldName_display()

可以使用这个函数返回显示名:ObjectName.get_FieldName_display()

Replace ObjectName with your class name and FieldName with the field of which you need to fetch the display name of.

将ObjectName替换为您的类名,并将FieldName替换为您需要获取显示名的字段。

#4


6  

If the form instance is bound, you can use

如果窗体实例被绑定,您可以使用

chosen_label = form.instance.get_FOO_display()

#5


3  

Here is a way I came up with. There may be an easier way. I tested it using python manage.py shell:

我想到了一个办法。也许有更简单的方法。我使用python管理测试了它。py壳:

>>> cf = ContactForm({'reason': 'feature'})
>>> cf.is_valid()
True
>>> cf.fields['reason'].choices
[('feature', 'A feature')]
>>> for val in cf.fields['reason'].choices:
...     if val[0] == cf.cleaned_data['reason']:
...             print val[1]
...             break
...
A feature

Note: This probably isn't very Pythonic, but it demonstrates where the data you need can be found.

注意:这可能不是非常python化的,但是它演示了可以在哪里找到所需的数据。

#6


2  

You can have your form like this:

你可以这样填写表格:

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

Then this would give you what you want:

这样你就能得到你想要的:

reason = dict(CHOICES)[form.cleaned_data["reason"]]

#7


0  

I think maybe @webjunkie is right.

我认为@webjunkie可能是对的。

If you're reading from the form from a POST then you would do

如果你正在阅读一篇文章中的表格,那么你应该这么做

def contact_view(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            contact = form.save()
            contact.reason = form.cleaned_data['reason']
            contact.save()

#1


58  

This may help:

这可以帮助:

reason = form.cleaned_data['reason']
reason = dict(form.fields['reason'].choices)[reason]

#2


105  

See the docs on Model.get_FOO_display(). So, should be something like :

请参阅Model.get_FOO_display()中的文档。所以,应该是:

ContactForm.get_reason_display()

In a template, use like this:

在模板中,如下所示:

{{ OBJNAME.get_FIELDNAME_display }}

#3


14  

This the easiest way to do this: Model instance reference: Model.get_FOO_display()

最简单的方法是:模型实例引用:Model. get_foo_display ()

You can use this function which will return the display name: ObjectName.get_FieldName_display()

可以使用这个函数返回显示名:ObjectName.get_FieldName_display()

Replace ObjectName with your class name and FieldName with the field of which you need to fetch the display name of.

将ObjectName替换为您的类名,并将FieldName替换为您需要获取显示名的字段。

#4


6  

If the form instance is bound, you can use

如果窗体实例被绑定,您可以使用

chosen_label = form.instance.get_FOO_display()

#5


3  

Here is a way I came up with. There may be an easier way. I tested it using python manage.py shell:

我想到了一个办法。也许有更简单的方法。我使用python管理测试了它。py壳:

>>> cf = ContactForm({'reason': 'feature'})
>>> cf.is_valid()
True
>>> cf.fields['reason'].choices
[('feature', 'A feature')]
>>> for val in cf.fields['reason'].choices:
...     if val[0] == cf.cleaned_data['reason']:
...             print val[1]
...             break
...
A feature

Note: This probably isn't very Pythonic, but it demonstrates where the data you need can be found.

注意:这可能不是非常python化的,但是它演示了可以在哪里找到所需的数据。

#6


2  

You can have your form like this:

你可以这样填写表格:

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

Then this would give you what you want:

这样你就能得到你想要的:

reason = dict(CHOICES)[form.cleaned_data["reason"]]

#7


0  

I think maybe @webjunkie is right.

我认为@webjunkie可能是对的。

If you're reading from the form from a POST then you would do

如果你正在阅读一篇文章中的表格,那么你应该这么做

def contact_view(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            contact = form.save()
            contact.reason = form.cleaned_data['reason']
            contact.save()