How to get the label of a choice in a Django forms ChoiceField?

webjunkie picture webjunkie · Apr 17, 2009 · Viewed 75.8k times · Source

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.

Answer

shacker picture shacker · Apr 18, 2009

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

ContactForm.get_reason_display()

In a template, use like this:

{{ OBJNAME.get_FIELDNAME_display }}