2012-02-07 83 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)