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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…