Django:Model Form“对象没有属性”clean_data“” [英] Django: Model Form "object has no attribute 'cleaned_data'"

查看:1677
本文介绍了Django:Model Form“对象没有属性”clean_data“”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的一个课程制作一个搜索表单。表单的模型是:

  from django import forms 
from django.forms import CharField,ModelMultipleChoiceField,ModelChoiceField
from books.models import Book,作者,类别

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]

我使用的视图是:



<$来自django.shortcuts的p $ p> import render_to_response,redirect,get_object_or_404
from django.template import RequestContext
from books.models import Book,作者
from books.forms import bookForm,SearchForm
from users.models import User

def search_book(request):
如果request.method ==POST:
form = SearchForm request.POST)
如果fo rm.is_valid():
form = SearchForm(request.POST)
stitle = form.cleaned_data ['title']
sauthor = form.cleaned_data ['author']
scategory = form.cleaned_data ['category']
else:
form = SearchForm()
返回render_to_response(books / create.html,{
form:form ,
},context_instance = RequestContext(request))

表单显示正常,但当我提交它时,我收到一个错误:'SearchForm'对象没有属性'clean_data'



我是不知道发生了什么,有人可以帮我吗?谢谢!

解决方案

由于某种原因,您在检查 is_valid()后重新实例化表单)。当 is_valid()已被调用时,窗体只得到一个 clean_data 属性,而您尚未在此新的,第二个例子。



只需摆脱第二个 form = SearchForm(request.POST),所有应该好吧。


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!

解决方案

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.

这篇关于Django:Model Form“对象没有属性”clean_data“”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆