2016-01-13 64 views
0

我在窗體中有一個名爲'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',) 

回答

2

嘗試茶NGE形式

hof = forms.BooleanField(required=False, help_text = 'hall of fame') 

hof = forms.BooleanField(initial=False, required=False, help_text = 'hall of fame') 
+0

仍然得到需要的複選框 – uche

+0

MultiValueDictKeyError在/簽名/ singlesigner/ 「 '霍夫'」 – uche

+0

嘗試視圖切換到該字段: 如果createsinglesigner_form.is_valid(): createsinglesigner_form.save() else: print creatinginglesigner_form.errors – zablotski

0

你不應該設置爲BooleanField表單上的初始值。

您也正在使用ModelForm,因此沒有必要在表單中重寫模型的邏輯。

forms.py:

class CreateSingleSignerForm(forms.ModelForm): 

    class Meta: 
     model = CreateSingleSigner 
     fields = (
      'hof', 
      ... 
     ) 

    # Instantiate the object and override the required setting on hof field: 
    def __init__(self, *args, **kwargs): 
     super(CreateSingleSignerForm, self).__init__(*args, **kwargs) 
     self.fields['hof'].required = True 

如果你想堅持到你的代碼,然後從forms.Form而不是forms.ModelForm繼承和指示Django的保存複選框的選中狀態:

hof = forms.BooleanField(required=True, help_text='Hall of Fame') 

爲了易讀性,在models.py中設置​​的默認值也是一種很好的做法。

相關問題