python – Django:要求勾选复选框以提交表格

前端之家收集整理的这篇文章主要介绍了python – Django:要求勾选复选框以提交表格前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在Django中创建一个表单(使用ModelForm).
有许多复选框,我想这样做,以便必须选择其中一个以提交表单.我不是指任何一个复选框,而是一个特定的框.我在Django文档中找不到任何内容.任何帮助,将不胜感激.

解决方法

就像是
from django import forms
class MyForm(forms.Form):
    check = forms.BooleanField(required = True)
    # your other form fields

对于BooleanField,required = True将检查是否选中该框.这是因为只有选中后才会提交数据.

资料来源:https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.BooleanField

Validates that the value is True (e.g. the check Box is checked) if the field has required=True.

猜你在找的Python相关文章