I am trying to find a way to display the text of the selected / starting value of a multiple select field.
My question is similar to this, except that I don't want the value I want to match the text of the variant that matches it:
Display value of django form field in 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 parameters (for example, [u'male ', u'female']. However, I somehow want to look at the string value from the key (for example, "Men", "Women"), something like:
{% for key in form.genders.value %} {{ form.genders.choices.key }} {% endfor %}
I canβt find a way to make this work. How can I achieve this with Django 1.3?
ps - This is a brief example, but I need to do this using larger dynamic lists that prohibit the use of multiple if statements (i.e., "if key ==" male "male, etc.)