Python Django:模型表单“对象没有属性‘cleaned_data’”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4308527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django: Model Form "object has no attribute 'cleaned_data'"
提问by Joseph
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' object has no attribute 'cleaned_data'
I'm not sure what's going on, can someone help me out? Thanks!
我不确定发生了什么,有人可以帮助我吗?谢谢!
采纳答案by Daniel Roseman
For some reason, you're re-instantiating the form after you check is_valid(). Forms only get a cleaned_dataattribute when is_valid()has been called, and you haven't called it on this new, second instance.
出于某种原因,您在检查后重新实例化表单is_valid()。表单只有cleaned_data在is_valid()被调用时才会获得一个属性,而您还没有在这个新的第二个实例上调用它。
Just get rid of the second form = SearchForm(request.POST)and all should be well.
只要摆脱第二个form = SearchForm(request.POST),一切都会好起来的。
回答by hughdbrown
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.
很像文档。
回答by subramanyam
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。

