加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 21:37:47 所属栏目:Python 来源:网络整理
导读:我正在Django中创建一个表单(使用ModelForm). 有许多复选框,我想这样做,以便必须选择其中一个以提交表单.我不是指任何一个复选框,而是一个特定的框.我在Django文档中找不到任何内容.任何帮助,将不胜感激. 解决方法 就像是 from django import formsclass MyF
我正在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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读