Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

From your template, add the following:

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

Do you see any errors from the above?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...