且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django:模型表单“对象没有属性‘cleaned_data’"

更新时间:2023-12-01 21:53:58

出于某种原因,您在检查 is_valid() 后重新实例化了表单.表单只有在 is_valid() 被调用时才获得一个 cleaned_data 属性,而你没有在这个新的第二个实例上调用它.

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.

去掉第二个 form = SearchForm(request.POST) 就可以了.

Just get rid of the second form = SearchForm(request.POST) and all should be well.