python - Django:需要勾选复选框以提交表单

标签 python django forms checkbox widget

我正在 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 - Django:需要勾选复选框以提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16988127/

相关文章:

Python MySQL 连接器未插入

django - django 理解 YML 吗? django 未加载固定装置 YML 文件(yml 不是已知的序列化)

php - 什么会导致 $_FILES ['file' ] ['name' ] 为空?

python - Django、Turbo Gears、Web2Py,哪个更好?

python - virtualenv(python3.4), pip install mysqlclient 错误

javascript - 单击按钮后消息出现在同一个 div 中

Django:用于排除第一个条目的循环

python - 如何按相关对象计数对查询集进行排序?

python - Django 模型序列化器

python - groupby() 给出一个空列表