AttributeError: type object 'Post' has no attribute 'query'

AttributeError: type object 'Post' has no attribute 'query'

我按照 Corey Schafer 的 flask 教程构建了一个社交媒体网站。然而,当我到了Create a post阶段,我遇到了"AttributeError: type object 'Post' has no attribute 'query'"但是我不明白为什么而Corey在视频中没有遇到这个问题......我真的很陌生我不知道出了什么问题,任何建议将不胜感激。

这是因为您没有在您的 model.py 中使用 Post(db.Model),它确实具有 query 方法,但您的程序正在使用 [=15] 中的 Post(FlaskForm) =].由于它们具有相同的名称,因此您的导入会相互覆盖:

# simplified
from sm.model import Post
from sm.forms import Post

尝试将它们明确命名为 PostModelPostForm 以便更容易区分两者。

(您会注意到这正是 Corey Schafers code snippets 中所做的)