JCoder96
JCoder96

Reputation: 188

Django: Required = False Not Working Properly

I have a Django server that I am working on for my company. For my web form, I want the user to be able to check a box, in which if selected, the user will have another document to sign. However, I do not want them to be required to check it.

The checkbox that I wish to have checked (or not) is the "callforwarding" field. I have tried setting the acc field as callforwarding=models.BooleanField(default=False, null=True, blank=True) In my forms.py, the field is: callforwarding=forms.BooleanField(required=false, widget=forms.CheckboxInput(attrs={'class' : 'form-control-lg'})) However, if the checkbox isn't checked, the form cannot be submitted. I don't receive any sort of error, the form just won't submit.

#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="[email protected]")
    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="[email protected]")
    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)

What am I doing wrong that is preventing me from making this checkbox optional?

Upvotes: 2

Views: 1279

Answers (2)

theQuestionMan
theQuestionMan

Reputation: 1562

I had this problem too...
I didn't have the model field as blank=True.
It worked once I set this. For example, in the model instead of:

email_list = models.CharField(max_length=5000)

I changed it to:

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

Upvotes: 0

JCoder96
JCoder96

Reputation: 188

I have no idea why, but by deleting, then adding back the lines in the form and model, the issue no longer occurs...

Upvotes: 1

Related Questions