Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Model Form "object has no attribute 'cleaned_data'"

I am trying to make a search form for one of my classes. The model of the form is:

from django import forms from django.forms import CharField, ModelMultipleChoiceField, ModelChoiceField from books.models import Book, Author, Category  class SearchForm(forms.ModelForm):     authors = ModelMultipleChoiceField(queryset=Author.objects.all(),required=False)         category = ModelChoiceField (queryset=Category.objects.all(),required=False)     class Meta:         model = Book         fields = ["title"] 

And the view I'm using is:

from django.shortcuts import render_to_response, redirect, get_object_or_404 from django.template import RequestContext from books.models import Book,Author from books.forms import BookForm, SearchForm from users.models import User  def search_book(request):     if request.method == "POST":         form = SearchForm(request.POST)         if form.is_valid():             form = SearchForm(request.POST)             stitle = form.cleaned_data['title']             sauthor = form.cleaned_data['author']             scategory = form.cleaned_data['category']     else:         form = SearchForm()     return render_to_response("books/create.html", {         "form": form,     }, context_instance=RequestContext(request)) 

The form shows up fine, but when I submit it I get an error: 'SearchForm' object has no attribute 'cleaned_data'

I'm not sure what's going on, can someone help me out? Thanks!

like image 835
Joseph Avatar asked Nov 29 '10 22:11

Joseph


People also ask

What is clean data in Django?

cleaned_data returns a dictionary of validated form input fields and their values, where string primary keys are returned as objects. form. data returns a dictionary of un-validated form input fields and their values in string format (i.e. not objects).

What is Is_valid Django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute. Let's see an example that takes user input and validate input as well. ADVERTISEMENT.


2 Answers

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.

like image 50
Daniel Roseman Avatar answered Oct 21 '22 01:10

Daniel Roseman


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.

like image 33
hughdbrown Avatar answered Oct 21 '22 00:10

hughdbrown