Django TextField always is required, despite blank=True,Null=True

≡放荡痞女 提交于 2020-01-14 12:42:09

问题


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.


回答1:


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.




回答2:


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!




回答3:


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.



来源:https://stackoverflow.com/questions/29956163/django-textfield-always-is-required-despite-blank-true-null-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!