首页 > 解决方案 > url_for 的烧瓶蓝图问题

问题描述

我在 Flask 中使用蓝图,我的蓝图是这样初始化的:

my_blueprint= Blueprint('my_blueprint', __name__,
    template_folder='templates',
    static_folder='static')

我想在我的 HTML 模板中显示图片,<img src="{{ picture.uri }}" alt="...">其中图片.uri 是图片的路径。os.path.join('/uploads/', f_name])我使用其中 f_name 是图片的文件名来构建此路径。

我的目录是这样构建的:

app
--templates
--static
--uploads
--__init__.py

我不明白如何在 HTML 模板中呈现我的图片。我需要在 img 标签的“src”字段中添加什么?

谢谢你的帮助。

标签: flaskblueprint

解决方案


您可以使用加载静态文件

{{ url_for('static', filename='path/to/file.jpg') }}

将从中加载文件PROJECT_ROOT/static/path/to/file.jpg

请记住,这仅用于开发用途,您必须在生产环境中从网络服务器解析资产。


推荐阅读