问题描述
我在 Flask 中使用蓝图,我的蓝图初始化如下:
my_blueprint= Blueprint('my_blueprint',__name__,template_folder='templates',static_folder='static')
我想使用 <img src="{{ picture.uri }}" alt="...">
在我的 HTML 模板中显示图片,其中 picture.uri 是图片的路径。我使用 os.path.join('/uploads/',f_name])
构建了这个路径,其中 f_name 是图片的文件名。
我的目录是这样构建的:
app
--templates
--static
--uploads
--__init__.py
我不明白如何在 HTML 模板中呈现我的图片。我需要在 img 标签的“src”字段中放入什么?
感谢您的帮助。
解决方法
您可以使用
加载静态文件{{ url_for('static',filename='path/to/file.jpg') }}
将从 PROJECT_ROOT/static/path/to/file.jpg
加载文件
请记住,这仅用于开发用途,您必须在生产环境中从网络服务器解析资产。