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的opencv还有face_r...
说到Pooling,相信学习过CNN的朋友们都不会感到陌生。Poolin...
记得大一学Python的时候,有一个题目是判断一个数是否是复数...
文章目录 3 直方图Histogramplot1. 基本直方图的绘制 Basic ...
文章目录 5 小提琴图Violinplot1. 基础小提琴图绘制 Basic v...
文章目录 4 核密度图Densityplot1. 基础核密度图绘制 Basic ...