我正在尝试在我的项目中实现 Flask-Uploads。但是,我遇到了麻烦,因为我正在使用应用程序工厂功能并且无法从应用程序中的其他文件访问 UploadSets!
文件:应用程序/__init__.py
from flask_uploads import UploadSet, configure_uploads, IMAGES
def create_app(object_name):
...
images = UploadSet('images', IMAGES)
configure_uploads(app, images)
文件:应用程序/控制器/版本.py
from flask_uploads import UploadNotAllowed
# this is one of my route files, registered in a blueprint
editions = Blueprint('editions', __name__)
...
# inside one of the routes
poster = request.files.get('poster')
try:
filename = images.save(poster)
except UploadNotAllowed:
flash('The upload was not allowed.', category='warning')
else:
# add to db...
当然这行不通,因为images
没有定义。但是如何从应用程序中导入它?
我试过:
import app
app.images.save(...)
from flask import current_app
current_app.images.save(...)
from app import images
没有工作。有任何想法吗???
PS:除此之外,我的应用程序中的其他所有内容都运行良好。