2
私はこのような何かを持っている:使用値は
forms.py:
views.py
:私は私の視界からform
を渡している
AVATAR_CHOICES = (('1','option1'), ('2','option2'), ('3','option3'),
('4','option4'))
class RegistrationForm(forms.Form):
avatar = ChoiceField(widget=RadioSelect, choices=AVATAR_CHOICES)
を
form = RegistrationForm()
return render_to_response('registration/register.html', {'form': form},
context_instance=RequestContext(request))
このようなテンプレートには、
registration.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 %}
どうすればいいですか?
ありがとうございます。
@ IgnacioVazquez-Abramsこの質問は、フォームフィールドのためのものですが、選択肢を持つモデルフィールドの質問です。 AFAIKフォームフィールドには対応する 'get_XXX_display'がありません。もしあれば、それは(a)別のものであり、(b)ここでそれをどのように扱うかがすぐには分かりません。 – Dougal
ハ。そして、ここで私はいつも質問を読んでいない人を非難している(私はそれについて常に間違っている*ではありません。 –