【问题标题】:Django: Required = False Not Working ProperlyDjango:必需 = False 无法正常工作
【发布时间】:2019-02-11 22:16:47
【问题描述】:

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

我希望选中(或不选中)的复选框是“呼叫转移”字段。我尝试将 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)

我做错了什么导致我无法将此复选框设为可选?

【问题讨论】:

    标签: django django-models django-forms


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      email_list = models.CharField(max_length=5000)
      

      我改成:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-21
        • 2014-03-07
        • 2013-02-25
        • 2015-01-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多