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'
表单显示的很好,但是当我提交它时,我得到一个错误:“SearchForm”对象没有属性“cleaned_data”
I'm not sure what's going on, can someone help me out? Thanks!
我不知道发生了什么事,有人能帮我一下吗?谢谢!
3 个解决方案
#1
121
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.
出于某种原因,您在检查is_valid()之后重新实例化表单。当is_valid()被调用时,表单只获得一个cleaned_data属性,而您还没有在这个新的、第二个实例上调用它。
Just get rid of the second form = SearchForm(request.POST)
and all should be well.
只要去掉第二个form = SearchForm(request.POST)就可以了。
#2
4
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.
很像文档。
#3
2
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
.
在django表单的clean函数中,我们没有任何数据,但是form.is_valid()将返回True。
#1
121
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.
出于某种原因,您在检查is_valid()之后重新实例化表单。当is_valid()被调用时,表单只获得一个cleaned_data属性,而您还没有在这个新的、第二个实例上调用它。
Just get rid of the second form = SearchForm(request.POST)
and all should be well.
只要去掉第二个form = SearchForm(request.POST)就可以了。
#2
4
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.
很像文档。
#3
2
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
.
在django表单的clean函数中,我们没有任何数据,但是form.is_valid()将返回True。