作者:手机用户2602916425 | 来源:互联网 | 2023-02-06 21:30
有没有人尝试过基于静态文件夹代码cnippet的片段瓶配置?
代码:
import flask
class MyFlask(flask.Flask):
@property
def static_folder(self):
if self.config.get('STATIC_FOLDER') is not None:
return os.path.join(self.root_path,
self.config.get('STATIC_FOLDER'))
@static_folder.setter
def static_folder(self, value):
self.config.get('STATIC_FOLDER') = value
# Now these are equivalent:
app = Flask(__name__, static_folder='foo')
app = MyFlask(__name__)
app.config['STATIC_FOLDER'] = 'foo'
就我而言,抱怨这条线:
self.config.get('STATIC_FOLDER') = value
错误消息: Can't assign to function call
有没有人如何在Flask中的config.py文件中设置static_folder?
1> 小智..:
好吧,我假设您想要使用静态文件夹的自定义路径,无论出于何种原因.为了更好的应用程序模块化,我想做同样的事情.
这是我的app文件夹结构:
instance/
core/
|_templates/
|_static/
|_views.py
run.py
config.py
如您所见,我的static
文件夹位于core
文件夹中.
在run.py
,您可以执行以下操作:
app = Flask(__name__, static_url_path=None)
if __name__ == '__main__':
app.config.from_object('config')
# config file has STATIC_FOLDER='/core/static'
app.static_url_path=app.config.get('STATIC_FOLDER')
# set the absolute path to the static folder
app.static_folder=app.root_path + app.static_url_path
print(app.static_url_path)
print(app.static_folder)
app.run(
host=app.config.get('HOST'),
port=app.config.get('PORT'),
threaded=True
)
这就是我所做的,它完美无缺.我正在使用烧瓶0.12.