I am trying to generate a form using django documentation. I am continously getting the error:
'TestForm' object has no attribute 'cleaned_data'
even though form.is_valid
is True
(it prints the 'form is valid' line of my code). Following are the relevant portions of my code.
urls.py
url(r'^test/',views.test),
forms.py
from django import forms
class TestForm(forms.Form):
name = forms.CharField()
views.py
def test(request):
if request.method == 'POST':
form = TestForm(request.POST)
if form.is_valid:
print 'form is valid'
print form.cleaned_data
else:
print 'form not valid'
else:
form = TestForm()
return render_to_response('User/Test.html',{'form': form},context_instance=RequestContext(request))
Test.html
<form action="" method="post">{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit">
</form>
You are not triggering the cleaning and validation of the form, this is made by calling the is_valid()
method (note the parentheses ()
), that's why you have no cleaned data.
Correction:
if request.method == 'POST':
form = TestForm(request.POST)
if form.is_valid():
print 'form is valid'
print form.cleaned_data
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With