I am using flask_uploads and am getting this error ImportError: cannot import name 'patch_request_class' from 'flask_uploads'

I am using flask_uploads and am getting this error ImportError: cannot import name 'patch_request_class' from 'flask_uploads'

我的代码是这样的

from flask_uploads import IMAGES, UploadSet, configure_uploads, patch_request_class
import os

basedir = os.path.abspath(os.path.dirname(__file__))
app.config['UPLOADED_PHOTOS_DEST'] = os.path.join(basedir, 'static/images')
photos = UploadSet("photos", IMAGES)

configure_uploads(app, photos)
patch_request_class(app)

可能是什么问题。我能否获得有关如何解决问题的任何帮助

最近,patch_request_class 已从 Flask-Reuploaded 中删除,它是 Flask-Uploads 的维护分支,安装为 flask_uploads 以保持兼容性(因此您没有需要更改导入)。

您确定使用 Flask-Uploads 而不是 Flask-Reuploaded 吗?看看你的 requirements.txt 或 setup.py.

这里是 commit,其中 patch_request_class 被删除。

如您所见,patch_request_class 已经弃用很长时间了,只需要将上传限制在 Flask 版本 0.6 以内。之后就可以使用Flask本身的MAX_CONTENT_LENGTH环境变量了,见https://flask.palletsprojects.com/en/1.1.x/config/#MAX_CONTENT_LENGTH

tl/dr

  • 删除 patch_request_class 导入
  • 移除patch_request_class(app)
  • MAX_CONTENT_LENGTH 设置为所需的值