Django:必填= False,无法正常工作

时间:2019-02-11 22:16:48

标签: django django-models django-forms

我有一个正在为公司工作的Django服务器。对于我的Web表单,我希望用户能够选中一个框,如果选中该框,则用户将要签名另一个文档。但是,我不希望他们被要求检查。

我希望选中(或不选中)的复选框是“ callforwarding”字段。我尝试将acc字段设置为callforwarding=models.BooleanField(default=False, null=True, blank=True),在我的forms.py中,该字段是:callforwarding=forms.BooleanField(required=false, widget=forms.CheckboxInput(attrs={'class' : 'form-control-lg'}))但是,如果未选中此复选框,则无法提交表单。我没有收到任何错误,表单不会提交。

#forms.py
class LOAForm(forms.Form):
    propertyname = forms.CharField(max_length = 40,
            widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'Name Of Property'}))
    signdate = forms.DateField(widget=forms.DateInput(attrs={'class' : 'form-control', 'placeholder' : 'yyyy-mm-dd'}))
    billingaddress = forms.CharField(max_length=20,
            widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'Billing Street Address'}))
    billingcity = forms.CharField(max_length=15,
            widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'Billing Address City'}))
    billingzipcode = forms.CharField(widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'Billing Address Zip Code'}))
    billingemail = forms.CharField(widget=forms.EmailInput(attrs={'class' : 'form-control', 'placeholder' : 'Email Address for Billing'}))
    streetaddress = forms.CharField(max_length=20,
            widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'Property Street Address'}))
    streetcity = forms.CharField(max_length=15,
            widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : ' Property City'}))
    streetzipcode = forms.CharField(widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'Property Zip Code'}))
    ainame = forms.CharField(max_length=30,
            widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'Authorized Individual Name'}))
    titleinbusiness = forms.CharField(max_length=20,
            widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'Title Of Individual in Business'}))
    phonenumber = forms.CharField(max_length=30,
            widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'Phone Number of Authorized Individual (e.g. 8008675309'}))
    callforwarding = forms.BooleanField(required=False, widget=forms.CheckboxInput(attrs={'class' : 'form-control-lg'}))
    mainnumber = forms.CharField(max_length=30,
            widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'Main Phone Number'}))
    acc = forms.BooleanField(required=False, widget=forms.CheckboxInput(attrs={'class' : 'form-control-lg'}))
    email = forms.CharField(widget=forms.EmailInput(attrs={'class': 'form-control', 'placeholder' : 'Email Address for Signature'}))
    portnums = forms.CharField(widget=forms.Textarea(attrs={'class' : 'form-control', 'placeholder' : 'Phone Numbers to be Ported (Comma Separated)'}))
    phonebill = forms.FileField(required=False, widget=forms.ClearableFileInput(attrs={'multiple' : True}))
    captcha = ReCaptchaField(public_key="6Lcn9ooUAAAAALIXQ1nOQuppT_fUbhx0ntP5onRX", private_key="6Lcn9ooUAAAAANWelTZA7IbG4RtpSepzEnR_m4xJ", attrs={'theme' : 'clean'})

#Models.py
class LOA(models.Model):
    propertyname = models.CharField(max_length=40)
    signdate = models.DateField(default = timezone.now)
    day = models.CharField(max_length=2, null=True)
    daysubscript = models.CharField(max_length=2, null=True)
    month = models.CharField(max_length=15, null=True)
    year = models.CharField(max_length=4, null=True)
    billingaddress = models.CharField(max_length=40)
    billingcity = models.CharField(max_length=40)
    billingstate = models.CharField(max_length=40)
    billingzipcode = models.CharField(max_length=10)
    billingemail = models.EmailField(default="username@example.com")
    streetaddress = models.CharField(max_length=40)
    streetcity = models.CharField(max_length=40)
    streetstate = models.CharField(max_length=40)
    streetzipcode = models.CharField(max_length=10)
    ainame = models.CharField(max_length=40)
    titleinbusiness = models.CharField(max_length=40)
    phonenumber = models.CharField(max_length=40)
    callforwarding = models.BooleanField(default=False, null=True, blank=True)
    mainnumber = models.CharField(max_length=40, null=True, blank=True)
    acc = models.BooleanField(default=False)
    portnums = models.TextField()
    email = models.EmailField(default="username@example.com")
    phonebill = models.FileField(upload_to='phonebills', null=True, blank=True)
    pdf = models.FileField(upload_to='pdfs/', null=True, blank=True)
    sa = models.FileField(upload_to='serviceagreements', null=True, blank=True)
    myquote = models.FileField(upload_to='phonebills', null=True, blank=True)
    def __str__(self):
            return '<billingname: {}, signdate: {}, ID: {}>'.format(self.propertyname, self.signdate, self.id)

我在做什么错,这使我无法将此复选框设置为可选?

2 个答案:

答案 0 :(得分:0)

我不知道为什么,但是通过删除然后再添加表单和模型中的行,该问题不再发生...

答案 1 :(得分:0)

我也有这个问题...
我没有将模型字段设为空白 = True。
一旦我设置了它,它就起作用了。例如,在模型中代替:

email_list = models.CharField(max_length=5000)

我改成:

email_list = models.CharField(max_length=5000, blank=True)