自定义静态文件夹的烧瓶蓝图无法使用 url_for 在模板中工作

flask blueprint for custom static folder not working in templates using url_for

我正在尝试使用蓝图从 /static 以外的文件夹中为我的一些 js 和 css 文件提供服务。我希望这些文件可以从根目录获得。我已经在应用程序中注册了以下蓝图:

custom = Blueprint('/', __name__, static_folder='custom', static_url_path='/custom')
app.register_blueprint(custom)

我可以在预期的位置找到所需的文件,即。 /custom/custom.css 通过将此 url 直接放入浏览器,但是,我无法使用 url_for 在模板中访问它们,如下所示:

<link href="{{ url_for('custom', filename='style.css') }}" rel="stylesheet">

我得到:BuildError: ('custom', {'filename': 'style.css'}, None)

我尝试了任意数量的组合,即:

custom = Blueprint('custom', __name__, static_folder='custom', static_url_path='/custom')

<link href="{{ url_for('custom.custom', filename='style.css') }}" rel="stylesheet">

等等,但对我没有任何作用。

您需要使用蓝图的静态端点。

url_for('custom.static', filename='custom.css')