文章目录
- 1. KeyError: 'A secret key is required to use CSRF.'
- 2. flask KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS'
- 3. UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
- 4. FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
- 5. sqlalchemy.exc.InvalidRequestError: Table 'user' is already defined for this MetaData instance. Specify 'extend_existing=True' to redefine options and columns on an existing Table object.
- 6. Warning: (3719, "'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.")
- 7. sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (3780, "Referencing column 'role_id' and referenced column 'id' in foreign key constraint 'admin_ibfk_1' are incompatible.")
- 8. RuntimeError: No application found. Either work inside a view function or push an application context.
- 9. ERROR [root] Error: Target database is not up to date.
- 10. RuntimeError: No application found. Either work inside a view function or push an application context
1. KeyError: ‘A secret key is required to use CSRF.’
问题来源:提交 Form 表单时候报错
问题解析:报错的字面意思是使用CSRF需要密钥,说明我们适用CSRF的时候没有带密钥
解决方法:在实例化Flask之后配置上 SECRET_KEY 参数:
app = Flask(__name__)
app.config["SECRET_KEY"] = "12345678"
2. flask KeyError: ‘SQLALCHEMY_TRACK_MODIFICATIONS’
问题来源:提交 Form 表单时候报错
问题解析:博主安装的是当前日期最新版的Flask-SQLAlchemy:v2.4.3,很多人加入app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True自然是解决不了问题的,说将Flask-SQLAlchemy降级规避也没有必要。报错的真正原因是models和views中的Flask实例对象时没有使用同一个Flask实例化,很多人实例化了两个Flask对象。注意即便使用同一个参数__name__来实例化Flask,得到的app对象也是不一样的,因为它们的内存地址不同。
解决方法:创建一个专门实例化Flask的Python文件,可以命名为create_app.py。models和views里都导入实例化好的app对象,那么它们使用的就是同一个Flask实例了。
from flask import