Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

django - form has no errors but form.is_valid() doesn't validate

I have a form that for some reason it it doesn't throw any errors (if user adds data in the text field) but form.is_valid() doesn't validate. Any ideas?

forms.py:

class MyForm(forms.Form):
    title = forms.CharField(widget=forms.TextInput(attrs={'class':'titleField')) 

mytemplate.html

  <form action="" method="post" name="form">{% csrf_token %}
  {{ form.title.errors }}
  {{ form.title }}
  <input type="submit" name='submit_button' value="Post" />
  </form>

views.py:

      if 'submit_button' in request.POST:
            form = MyForm(request.POST)
            if form.is_valid():
               cd = form.cleaned_data 
               title = cd['title']
               g = MyData(title='title')   
               g.save()

       else:
            form = MyForm() 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From your template, add the following:

{{ form.errors }}
{{ form.non_field_errors }}

Do you see any errors from the above?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...