【发布时间】:2011-10-02 03:16:24
【问题描述】:
在尝试了几个小时后,我对此感到沮丧。我只是无法在模板中循环我的 ChoiceField 的选择。它甚至不会进入循环。但是如果我使用 pdb 访问表单字段,它看起来很好。
我的表格:
MODE_CHOICES = (('blue', 'blue'), ('red', 'red'))
class MultiSearchForm(forms.Form):
mode = forms.ChoiceField(required = True, widget = RadioSelect, choices = MODE_CHOICES)
我的看法:
class LandingPage(TemplateView):
template_name = "landingPage.html"
def get_context_data(self, **kwargs):
context = super(LandingPage, self).get_context_data(**kwargs)
context.update({
'searchForm': MultiSearchForm(),
})
return context
我的模板:
<ul>
{% for choice in searchForm.mode.choices %} // for loop is not entered
<li>
<input type="radio" name="mode" value="{{choice.0}}"
{% ifequal searchForm.mode.data choice.0 %}
checked="checked"
{% endifequal %}/>
</li>
{% endfor %}
</ul
{{searchForm.mode.choices.0}} //no output
{{searchForm.mode}} // gives me 2 radio buttons
【问题讨论】:
标签: django forms templates choicefield