Django: Model Form "object has no attribute 'cleaned_data'"
For some reason, you're re-instantiating the form after you check is_valid()
. Forms only get a cleaned_data
attribute when is_valid()
has been called, and you haven't called it on this new, second instance.
Just get rid of the second form = SearchForm(request.POST)
and all should be well.
I would write the code like this:
def search_book(request): form = SearchForm(request.POST or None) if request.method == "POST" and form.is_valid(): stitle = form.cleaned_data['title'] sauthor = form.cleaned_data['author'] scategory = form.cleaned_data['category'] return HttpResponseRedirect('/thanks/') return render_to_response("books/create.html", { "form": form, }, context_instance=RequestContext(request))
Pretty much like the documentation.
At times, if we forget the
return self.cleaned_data
in the clean function of django forms, we will not have any data though the form.is_valid()
will return True
.