从 Flask 中的配置更改静态文件夹 [英] Change static folder from config in Flask

查看:22
本文介绍了从 Flask 中的配置更改静态文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人试过这个片段基于flask配置的静态文件夹代码cnippet?

Has anyone tried this snippet flask config based static folder code 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'

就我而言,抱怨这条线:

In my case in complains about this line:

self.config.get('STATIC_FOLDER') = value

错误信息:无法分配给函数调用

有人知道如何从 Flask 中的 config.py 文件中设置 static_folder 吗?

Does anyone how to set the static_folder from the config.py file in Flask?

推荐答案

我对那个片段一无所知,但是

I don't know anything about that snippet, but

some_function(...) = some_value

永远不是有效的 Python(Python 没有 l 值).看起来 config 有一个类似 dict 的界面,所以违规行应该是

is never valid Python (Python doesn't have l-values). It looks like config has a dict-like interface, so the offending line should probably just be

self.config['STATIC_FOLDER'] = value

可能是 setter 上方的 getter 定义中的复制粘贴错误.

Probably a copy-and-paste error from the getter definition above the setter.

这篇关于从 Flask 中的配置更改静态文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆