我正在Django中创建一个表单(使用ModelForm).
有许多复选框,我想这样做,以便必须选择其中一个以提交表单.我不是指任何一个复选框,而是一个特定的框.我在Django文档中找不到任何内容.任何帮助,将不胜感激.
有许多复选框,我想这样做,以便必须选择其中一个以提交表单.我不是指任何一个复选框,而是一个特定的框.我在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.