2012-02-07 75 views
0

我知道Django Dev版本將使this addition更容易,但是有什麼方法可以自定義當前版本的django中單選按鈕的選項字段嗎?評論版本是我期待做的。問題是,當表單無效時,表單會在沒有輸入任何原始條目的情況下重新加載。Django 1.3.1中的自定義選擇標籤和顯示

<div class="category box"> 
    <p class="question">Category:</p> 
    {{ form.category.errors }} 
    {{ form.category }} 
    {% comment %} 
    <ul> 
    <li><label for="id_category_0"><input type="radio" id="id_category_0" value="E" name="category"><img src="{{ MEDIA_URL }}images/icons/eating.png"><br />Eating</label></li> 
    <li><label for="id_category_1"><input type="radio" id="id_category_1" value="D" name="category"><img src="{{ MEDIA_URL }}images/icons/drinking.png"><br />Drinking</label></li> 
    <li><label for="id_category_2"><input type="radio" id="id_category_2" value="S" name="category"><img src="{{ MEDIA_URL }}images/icons/sleeping.png"><br />Sleeping</label></li> 
    <li><label for="id_category_3"><input type="radio" id="id_category_3" value="P" name="category"><img src="{{ MEDIA_URL }}images/icons/exploring.png"><br />Exploring</label></li> 
    <li><label for="id_category_4"><input type="radio" id="id_category_4" value="O" name="category"><img src="{{ MEDIA_URL }}images/icons/other.png"><br />Other</label></li> 
    </ul> 
    {% endcomment %} 
</div> 
<div class="rating box"> 
    <p class="question">How would you rate it??</p> 
    {{ form.rating.errors }} 
    {{ form.rating }} 
    {% comment %} 
    <ul> 
     <li><label for="id_rating_0"><input type="radio" id="id_rating_0" value="1" name="rating"><span class="glyph" style="color: lightgreen;">j</span> Must Do</label></li> 
     <li><label for="id_rating_1"><input type="radio" id="id_rating_1" value="2" name="rating"><span class="glyph" style="color: lightblue;">l</span> Do</label></li> 
     <li><label for="id_rating_2"><input type="radio" id="id_rating_2" value="3" name="rating"><span class="glyph">L</span> Miss</label></li> 
    </ul> 
    {% endcomment %} 
</div> 

回答

1

的問題是,你繞過窗口小部件,這意味着它不知道如何顯示當前選擇的價值,呈現邏輯。

你可以做一些黑客行爲並檢查模板本身中的表單數據。但更好的方法是子類forms.widgets.RadioFieldRenderer並編寫您自己的渲染功能。在這個例子中,我已經完成了它在自己的<td>中呈現每個無線電輸入,我做了一個多選題測驗,其中所有問題都具有相同的答案格式。

from django.utils.safestring import mark_safe 
from django.utils.encoding import force_unicode 
from django.utils.html import conditional_escape 

class ReportRadioInput(forms.widgets.RadioInput): 
    def __unicode__(self): 
     return mark_safe(u'%s' % self.tag()) 


class ReportRadioRenderer(forms.widgets.RadioFieldRenderer): 
    def __iter__(self): 
     for i, choice in enumerate(self.choices): 
      yield ReportRadioInput(self.name, self.value, self.attrs.copy(), choice, i)  

    def render(self): 
     """Outputs a <ul> for this set of radio fields.""" 
     return mark_safe(u'\n'.join([u'<td class="choice">%s</td>' 
       % force_unicode(w) for w in self])) 


class AnswerForm(forms.Form): 
    def __init__(self, *args, **kwargs): 
     questionnaire = kwargs.pop('questionnaire') 
     super(AnswerForm, self).__init__(*args, **kwargs)  

     self.fieldsets = [] 

     for g in questionnaire.groups.all(): 
      fields = [] 

      for q in g.questions.all(): 
       fieldname = 'question_%i' % q.pk 
       widget = forms.RadioSelect(renderer=ReportRadioRenderer) 
       self.fields[fieldname] = forms.TypedChoiceField(coerce=int, empty_value=None, required=True, label=q.text, choices=CHOICES, widget=widget) 
       fields.append(self[fieldname]) 
      self.fieldsets.append(dict(legend=g.name,fields=fields)) 

,將你感興趣的是ReportRadioRenderer位,並採用widget = forms.RadioSelect(renderer=ReportRadioRenderer)