2013-04-16 388 views
0

我正在开发django中的站点。我必须在模板中显示复选框。如何在django中创建复选框

我使用模型的形式,

class ReportPersonForm(forms.ModelForm): 
    class Meta: 
     model = ReportPerson 
     fields = ['name','first_aid','sick_bay','ambulance'] 

我要分别创建以下字段复选框,first_aid,sick_bay,救护车和模板渲染。

任何人都可以帮助我在django中创建一个检查以及如何设计模板来显示复选框。

感谢

+0

如果这些字段在您的模型中定义为BooleanField,那么它们应该在模型中呈现为复选框 –

+0

,因为它们被定义为BooleanField,所以如何编写模板以在页面中显示它 – user2086641

回答

2

如果您定义字段在模型中为BooleanFileld - 您可以使用

{{ form.first_aid }} 

in your template

0

在您的表单文件

class ReportPersonForm(forms.ModelForm): 
    first_aid = models.BooleanField() 
    sick_bay = models.BooleanField() 
    ambulance = models.BooleanField() 
    class Meta: 
     model = ReportPerson 
     fields = ['name','first_aid','sick_bay','ambulance'] 

然后为您的模板,你有多种选择,在文档阅读什么适合你的需要: Working with forms