flask - 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”字段中添加什么?
谢谢你的帮助。
解决方案
您可以使用加载静态文件
{{ url_for('static', filename='path/to/file.jpg') }}
将从中加载文件PROJECT_ROOT/static/path/to/file.jpg
请记住,这仅用于开发用途,您必须在生产环境中从网络服务器解析资产。
推荐阅读
- asp.net - .aspx 在 URL 的末尾
- css - 图标不显示?
- html - 旋转元素时,具有透明背景的 div 中出现意外的灰线
- python - 如何在 Jupyter Notebook 单元格中刷新 Python 导入?
- java - java模块问题“模块'mp3player'从'javafx.graphics'和'javafx.graphics'读取包'javafx.animation'”
- python - 从 Python 中的嵌套列表中获取一个集合
- python - FileNotFoundError:[Errno 2] 没有这样的文件或目录:hparams.json
- c# - Blazor WASM Cookie 身份验证 .NET5
- node.js - 使用 GRPC 接口获取比特币现金钱包地址的交易事件
- ios - iOS AVFoundation:获取 AVCaptureDevice.Format 的视频尺寸