I am having trouble with a field that seems to always want to be required despite my best wishes. My 'word_search' text field is always requesting data to be input but I have been trying to make sure the options allow for a blank.
my model is this. You can see the blank=True,Null=True options
class IAV(models.Model):
z_score = models.DecimalField(max_digits = 4,decimal_places=4)
screens = models.IntegerField(default=0)
flu_proteins = models.IntegerField(default = 0)
Key_word = models.TextField(blank=True,null=True)
sess = models.ForeignKey(Sess_IAV,default=None)
my view is this
def new_IAV(request):
if request.method == "POST":
form = IAVForm(request.POST,request.FILES)
if form.is_valid():
sess_ = Sess_IAV.objects.create()
form.save(
for_page=sess_,
z_score = form.cleaned_data("z_score"),
screens = form.cleaned_data("screens"),
flu_proteins = form.cleaned_data("flu_proteins"),
Key_word = form.cleaned_data("key_word"),
)
return redirect(sess_)
else:
print(form.errors)
else:
url=reverse('IAV_home')
return HttpResponseRedirect(url)
My form is this. you can see the required=False attribute.
class IAVForm(forms.models.ModelForm):
z_score = forms.DecimalField(widget=forms.NumberInput(attrs={'class':'form-control','value':'0.0',}))
screens = forms.IntegerField(widget=forms.NumberInput(attrs={'class':'form-control','value':'0',}))
flu_proteins = forms.IntegerField(widget=forms.NumberInput(attrs={'class':'form-control','value':'0',}))
key_word = forms.CharField(widget=forms.Textarea(attrs={'class':'form-control','rows':1,'cols':10,'placeholder':'keword values','required':'False'}))
class Meta:
model=IAV
fields=('z_score','screens','flu_proteins','key_word')
def save(self,for_page,z_score,screens,flu_proteins,key_word):
self.instance.sess = for_page
self.instance.z_score = z_score
self.instance.screens = screens
self.instance.flu_proteins = flu_proteins
self.instance.key_word = key_word
return super().save()
I am not sure how this field is not allowed to be left blank considering the model has the 'blank=True, null=True' options present.
Also the widget says that it isn't reqired.
Try this:
class IAVForm(forms.ModelForm):
z_score = forms.DecimalField(widget=forms.NumberInput(attrs={'class':'form-control','value':'0.0',}))
screens = forms.IntegerField(widget=forms.NumberInput(attrs={'class':'form-control','value':'0',}))
flu_proteins = forms.IntegerField(widget=forms.NumberInput(attrs={'class':'form-control','value':'0',}))
key_word = forms.CharField(required=False, widget=forms.Textarea(attrs={'class':'form-control','rows':1,'cols':10,'placeholder':'keword values'}))
To begin, you can just make the class (forms.ModelForm).
Additionally, you had required=False
inside of quotes and as an attribute. Remove the quotes and put it before the attributes of the widget.
See if that works.
Try this way.
def __init__(self, *args, **kwargs):
super(IAVForm, self).__init__(*args, **kwargs)
self.fields["key_word"].required = False
Also I noticed that key_word
is not in your model fields!
Try removing the required property completely from the field attributes. Required="True/False"
is not valid. If you want to disable it you have to remove it completely, per the W3C Specs and this article .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.