简体   繁体   中英

How to create check box in django

I am developing a site in django.I have to display a check box in templates.

I am using model form,

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

I have to create check box for the following fields respectively,first_aid,sick_bay,ambulance and render in template.

Can anyone help me to create a check in django and how to design template to display the check box.

Thanks

If you define you field in model as BooleanFileld - you can use

{{ form.first_aid }}

in you template

In your forms file

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']

Then for your template you have multiple options, read on in the docs what suits your needs: Working with forms

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM