Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form object has no attribute 'cleaned_data'

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>
like image 753
Jisson Avatar asked Nov 11 '11 07:11

Jisson


1 Answers

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
    ...
like image 66
juliomalegria Avatar answered Sep 24 '22 01:09

juliomalegria