2012-05-08 8 views
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 %} 

どうすればいいですか?

ありがとうございます。

+0

@ IgnacioVazquez-Abramsこの質問は、フォームフィールドのためのものですが、選択肢を持つモデルフィールドの質問です。 AFAIKフォームフィールドには対応する 'get_XXX_display'がありません。もしあれば、それは(a)別のものであり、(b)ここでそれをどのように扱うかがすぐには分かりません。 – Dougal

+0

ハ。そして、ここで私はいつも質問を読んでいない人を非難している(私はそれについて常に間違っている*ではありません。 –

答えて

6

あなただけchoice_labelgets documented

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

を試みることができる、しかし、あなたは安全にこれまでのところ、それを使用することができます。