无法在 Flask 中访问 app.instance_path
Cannot access app.instance_path in Flask
我正在尝试在我的 Flask 应用程序中实现一个 file upload 函数,但后来我尝试:
f.save(os.path.join(app.instance_path, 'imgs', filename))
在我的 class 中,我不断收到错误。第一次,我得到:
NameError: name 'app' is not defined
然后我尝试使用 import app
导入应用程序并得到以下信息:
AttributeError: module 'app' has no attribute 'instance_path'
我正在使用 Flask-Foundation 作为框架,并有一个 Flask 应用程序工厂创建我的应用程序(使用名称 app
)。使用 Flask 版本 0.12.2.
我应该在 settings.py 文件中指定 instance_path 吗?但是,正如 documentation 所说:
You can either explicitly provide the path of the instance folder when creating the Flask application or you can let Flask autodetect the instance folder.
所以我想我不需要指定它,我的命名可能有问题....有什么想法吗?
谢谢!
丹尼尔
是,通过更正导入修复:
import appname.app
然后:
f.save(os.path.join(appname.app.instance_path, 'imgs', filename))
(当然 appname
应该是您应用的任何名称...)
我正在尝试在我的 Flask 应用程序中实现一个 file upload 函数,但后来我尝试:
f.save(os.path.join(app.instance_path, 'imgs', filename))
在我的 class 中,我不断收到错误。第一次,我得到:
NameError: name 'app' is not defined
然后我尝试使用 import app
导入应用程序并得到以下信息:
AttributeError: module 'app' has no attribute 'instance_path'
我正在使用 Flask-Foundation 作为框架,并有一个 Flask 应用程序工厂创建我的应用程序(使用名称 app
)。使用 Flask 版本 0.12.2.
我应该在 settings.py 文件中指定 instance_path 吗?但是,正如 documentation 所说:
You can either explicitly provide the path of the instance folder when creating the Flask application or you can let Flask autodetect the instance folder.
所以我想我不需要指定它,我的命名可能有问题....有什么想法吗?
谢谢! 丹尼尔
是,通过更正导入修复:
import appname.app
然后:
f.save(os.path.join(appname.app.instance_path, 'imgs', filename))
(当然 appname
应该是您应用的任何名称...)