我正在尝试将“true / false”复选框的默认值更改为另一个。
不幸的是。我收到一个表单错误:“选择不是可用选项之一”。
请告诉我,我做错了什么?
models.py
CHECKBOX_CHOICES = (('1', 'The first choice'), ('2', 'The Second Choice'))
class Order(models.Model):
paid = models.CharField(max_length=350, choices=CHECKBOX_CHOICES)
forms.py
from django import forms
from .models import Order
class OrderCreateForm(forms.ModelForm):
class Meta:
model = Order
fields = ['paid']
widgets = {
'paid': forms.CheckboxSelectMultiple()
}
如果我将forms.CheckboxSelectMultiple()
更改为forms.CheckboxInput()
,则会收到表单错误:
Select the correct value. True is not one of the choices available.
create.html上
<form action="." method="post" class="order-form">
{{ form.as_ul }}
<p><input type="submit" value="Submit"></p>
{% csrf_token %}
</form>
呈现HTML
<ul id="id_paid">
<li><label for="id_paid_0"><input type="checkbox" name="paid" value="" checked="" id="id_paid_0">
---------</label>
</li>
<li><label for="id_paid_1"><input type="checkbox" name="paid" value="1" id="id_paid_1">
The first choice</label>
</li>
<li><label for="id_paid_2"><input type="checkbox" name="paid" value="2" id="id_paid_2">
The Second Choice</label>
</li>
</ul>
这对我来说很重要,即使这是一件小事。