Python 对象没有属性';清理数据';
我正在从事CS50 django项目,但我一直收到“objecthasnoattribute'cleaned_data”错误。我已经读了所有相关的问题,但仍然不能解决我的问题 这是我的view.py脚本Python 对象没有属性';清理数据';,python,django,Python,Django,我正在从事CS50 django项目,但我一直收到“objecthasnoattribute'cleaned_data”错误。我已经读了所有相关的问题,但仍然不能解决我的问题 这是我的view.py脚本 class NewTitleForm(forms.Form): newtitle = forms.CharField(label="title ") class NewContentForm(forms.Form): newcontent = forms.Char
class NewTitleForm(forms.Form):
newtitle = forms.CharField(label="title ")
class NewContentForm(forms.Form):
newcontent = forms.CharField(widget=forms.Textarea(attrs={"rows":5, "cols":5}))
def newpage(request):
if request.method == "POST":
titleinput = NewTitleForm(request.POST)
contentinput = NewContentForm(request.POST)
if titleinput.is_valid():
newtitle = titleinput.cleaned_data["newtitle"]
newcontent = contentinput.cleaned_data["newcontent"]
util.save_entry(newtitle,newcontent)
else:
return render(request, "encyclopedia/newpage.html", {
"NewTitleForm": NewTitleForm(),
"NewContentForm": NewContentForm()
})
return render(request, "encyclopedia/newpage.html",{
"NewTitleForm": NewTitleForm(),
"NewContentForm": NewContentForm()
})
错误代码是
AttributeError: 'NewContentForm' object has no attribute 'cleaned_data'
我不知道发生了什么你应该调用
contentinput.is\u valid()
像这样:
def newpage(request):
if request.method == "POST":
titleinput = NewTitleForm(request.POST)
contentinput = NewContentForm(request.POST)
if titleinput.is_valid() and contentinput.is_valid():
newtitle = titleinput.cleaned_data["newtitle"]
newcontent = contentinput.cleaned_data["newcontent"]
util.save_entry(newtitle,newcontent)
else:
return render(request, "encyclopedia/newpage.html", {
"NewTitleForm": NewTitleForm(),
"NewContentForm": NewContentForm()
})
return render(request, "encyclopedia/newpage.html",{
"NewTitleForm": NewTitleForm(),
"NewContentForm": NewContentForm()
})
清除的\u数据
将不存在,除非调用表单方法有效
您的新内容表单
类继承自表单。表单
类。因此,当您尝试访问cleaned_data
属性时,它会在NewContentForm
上或从forms.Form
继承来查找class属性。如果该属性在两个类中都不存在(并且在当前的NewContentForm
中也不存在),那么它将导致该属性错误。因此,您需要确认cleaned_data
是forms.Form
上存在的属性,或者将其添加到NewContentForm
类中
什么是完整的回溯?文件“C:\wiki\encyclopedia\views.py”,第36行,在newpage newcontent=contentinput.cleaned_data[“newcontent”]AttributeError:“NewContentForm”对象没有“cleaned_data”属性,它可以工作,thxxx