NameError: name 'db' is not defined、NameError: name 'User' is not defined(关键词:flask/bug)

来源:互联网 发布:微单 知乎 编辑:程序博客网 时间:2024/06/11 05:00

报错信息:

>>> dbTraceback (most recent call last):  File "<console>", line 1, in <module>NameError: name 'db' is not defined>>> UserTraceback (most recent call last):  File "<console>", line 1, in <module>NameError: name 'User' is not defined

出错来源:
《Flask Web开发》5.10 集成Python Shell,P56。

上百度搜到的结果,点开其中几个,从其中的一个,【I think you probably need to run some of the other code first so that you define db and your table schema. Then you can run db.create_all().】受到启发,应该是有某步代码没有执行,导致出现了错误。赶紧往回翻书。翻到5.8.1 创建表(P50),这里有:

(venv) $ python hello.py shell>>> from hello import db>>> db.create_all()

明白了,执行了导入db和User的动作之后,这个问题没了。

参考文献:
1.《Flask Web开发》。
2.SQLAlchemy NameError: Name ‘db’ is not defined (?)。

阅读全文
0 0
原创粉丝点击