Question

I have something like this:

forms.py:

AVATAR_CHOICES = (('1','option1'), ('2','option2'), ('3','option3'), 
('4','option4'))

class RegistrationForm(forms.Form):
    avatar = ChoiceField(widget=RadioSelect, choices=AVATAR_CHOICES)

I'm passing form from my view:

views.py

form = RegistrationForm()
return render_to_response('registration/register.html', {'form': form},
                          context_instance=RequestContext(request))

into a template like this:

registration.html:

{% for radio in form.avatar %}
  <label class="radio">{{ radio.tag }}</label>
{% endfor %}

but I'd like to include images in front of these option fields as a way to let the users choose an avatar. However, I don't know how to access keys or values from the tuple AVATAR_CHOICES. The main idea is to be able to do something like this:

{% for radio in form.avatar %}
      <label class="radio">{{ radio.tag }}</label>
      <img src="{{ STATIC_URL }}img/avatars/{{ radio.tag.value }}.jpg"/>
{% endfor %}

How can I do this?

Thanks in advance.

Was it helpful?

Solution

You could try

{{ radio.choice_value }}{# value of the choice of the current input #}
{{ radio.choice_label }}{# label of the choice of the current input #}
{{ radio.choice_index }}{# 0-based index #}

Only choice_label gets documented, but you could safely use it so far.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top