django checkboxSelectMultiple

时间:2018-05-08 17:30:52

标签: django-forms django-templates

发生了一些非常奇怪的事情,我在forms.py中构建了一个MultipleChoiceField,它正在呈现为普通列表。我无法显示复选框。我希望有人可以发现我可能出错的地方。

forms.py

from django import forms
from . import models
from behaviour.models import Interventions

class IncidentForm(forms.Form):

    def __init__(self,*args,**kwargs):
        self.request = kwargs.pop('request')
        super(IncidentForm,self).__init__(*args, **kwargs)

        intervention_set = Interventions.objects.filter(schoolid_id = self.request)
        intervention_choice = []
        for intervention in intervention_set:
            intervention_choice.append((intervention.pk, intervention.name))
        self.fields['intervention'].choices = intervention_choice

    intervention = forms.MultipleChoiceField(label='Intervention', choices=(), widget=forms.CheckboxSelectMultiple(), required=True,)

incident.html

<div>
    <label class="control-label">{% trans 'Intervention' %}</label><br />
    {{ form.intervention }}
    <small class="form-control-feedback"> {{ form.intervention.errors }} </small> 
</div>

HTML输出

<div>
    <label class="control-label">Intervention</label><br>
    <ul id="id_intervention">
        <li><label for="id_intervention_0"><input type="checkbox" name="intervention" value="3" id="id_intervention_0">
        Communicate verbally with Parent</label>
        </li>
        <li><label for="id_intervention_1"><input type="checkbox" name="intervention" value="2" id="id_intervention_1">
        Non-verbal signal</label>
        </li>
        <li><label for="id_intervention_2"><input type="checkbox" name="intervention" value="1" id="id_intervention_2">
        Spoke with Student</label>
        </li>
    </ul>
    <small class="form-control-feedback">  </small> 
</div>

Screenshot of output

0 个答案:

没有答案