I'm trying to find a way of displaying the text of the selected/initial value of a multiple choice field.
My question is similar to this one except I don't want the value I want to the option text it corresponds to:
Display value of a django form field in a template?
For example if I had a form with the following:
GENDER_CHOICES = (
('male', _('Men')),
('female', _('Women')),
)
genders = forms.MultipleChoiceField(choices=GENDER_CHOICES,
widget=widgets.CheckboxSelectMultiple(),
initial=[gender[0] for gender in GENDER_CHOICES])
then in my template I can do:
{{ form.genders.value }}
to get an array of selected options (i.e. [u'male', u'female']. However, I somehow want to look up the string value from the key (i.e. "Men", "Women"), something like:
{% for key in form.genders.value %}
{{ form.genders.choices.key }}
{% endfor %}
I can't find a way of making this work. How can I achieve this using Django 1.3?
ps - This is a short example, but I need to do it with bigger dynamic lists that prohibit using multiple if statements (i.e. "if key == 'male' 'Men' ... etc")