我在窗體中有一個名爲'hof'的複選框按鈕,如果我單擊窗體中的複選框併發送,它可以工作,但是如果我取消選中它拒絕發送,得到是這個字段是必需的。如何在django中設置一個布爾值爲False
這裏是我的模型
class CreateSingleSigner(models.Model):
firstname = models.CharField(max_length = 32)
middlename = models.CharField(max_length = 32)
lastname = models.CharField(max_length = 32)
displayname = models.CharField(max_length = 32)
era = models.CharField(max_length = 11)
category = models.CharField(max_length = 32)
hof = models.BooleanField()
externallink = models.CharField(max_length = 70)
loaprice = models.FloatField(max_length = 10)
basiccertprice = models.FloatField(max_length = 10)
appraisalcost = models.FloatField(max_length = 10)
notability = models.CharField(max_length = 32)
這裏,如果我的看法
def singlesigner(request):
context = {}
if request.method == 'POST':
createsinglesigner_form = CreateSingleSignerForm(data=request.POST)
if createsinglesigner_form.is_valid():
createsinglesigner = createsinglesigner_form.save(commit=False)
createsinglesigner.firstname = request.POST['firstname']
createsinglesigner.middlename = request.POST['middlename']
createsinglesigner.lastname = request.POST['lastname']
createsinglesigner.displayname = request.POST['displayname']
createsinglesigner.era = request.POST['era']
createsinglesigner.category = request.POST['category']
createsinglesigner.hof = request.POST['hof']
createsinglesigner.externallink = request.POST['externallink']
createsinglesigner.loaprice = request.POST['loaprice']
createsinglesigner.basiccertprice = request.POST['basiccertprice']
createsinglesigner.appraisalcost = request.POST['appraisalcost']
createsinglesigner.notability = request.POST['notability']
createsinglesigner_form.save()
else:
print createsinglesigner_form.errors
else:
# context['createsinglesigner'] = CreateSingleSigner()
createsinglesigner_form =CreateSingleSignerForm()
return render(request, "signer/singlesigner.html", {"createsinglesigner_form":createsinglesigner_form})
這裏是我的表
class CreateSingleSignerForm(forms.ModelForm):
firstname = forms.CharField(max_length = 32, required = True, help_text = "firstname")
middlename = forms.CharField(max_length = 32, required = True, help_text = "middlename")
lastname = forms.CharField(max_length = 32, required = True, help_text = "lastname")
displayname = forms.CharField(max_length = 32, required = True, help_text = "displayname")
era = forms.CharField(max_length = 11, required = True, help_text = "era")
category = forms.CharField(required = True, help_text = 'category')
hof = forms.BooleanField(required=False, help_text = 'hall of fame')
notability = forms.CharField(max_length = 11, required = True, help_text = 'notability')
externallink = forms.CharField(required = True, help_text = "externallink")
loaprice = forms.FloatField(required = True, help_text = "loaprice $")
basiccertprice = forms.FloatField(help_text = "basiccertprice $")
appraisalcost = forms.FloatField(help_text = "appraisalcost $")
class Meta:
model = CreateSingleSigner
fields = ('firstname',)
仍然得到需要的複選框 – uche
MultiValueDictKeyError在/簽名/ singlesigner/ 「 '霍夫'」 – uche
嘗試視圖切換到該字段: 如果createsinglesigner_form.is_valid(): createsinglesigner_form.save() else: print creatinginglesigner_form.errors – zablotski