2

私はこのようなものを持っています:

forms.py:

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

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

私はform私の見解から通り過ぎています:

views.py

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

このようなテンプレートに:

register.html:

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

ただし、ユーザーがアバターを選択できるようにする方法として、これらのオプションフィールドの前に画像を含めたいと思います。ただし、タプルからキーまたは値にアクセスする方法がわかりませんAVATAR_CHOICES。主なアイデアは、次のようなことができるようにすることです。

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

これどうやってするの?

前もって感謝します。

4

1 に答える 1

6

あなたは試すことができます

{{ 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 #}

choice_label 文書化されるだけですが、これまでのところ安全に使用できます。

于 2012-05-08T14:34:45.930 に答える