python - 在 Jinja2 中获取错误的值
问题描述
我在 Django 中将字典从 python 传递到我的 HTML,然后我想获取特定键的值。我在 for 循环中传递字典,然后使用 if 语句搜索键。如果找到该键,则打印该值。
以下是通过的字典:
{
'id': 1,
'book': {
'isbn': '0439554934',
'isbn13': '9780439554930',
'title': "Harry Potter and the Sorcerer's Stone (Harry Potter, #1)",
'publisher': 'Scholastic Inc',
'publishYear': 1997,
'publishMonth': 6,
'edition': None,
'rating': '4.46',
'popularity': 25779963,
'authors': ['J.K. Rowling'],
'genres': ['fiction', 'fantasy', 'young-adult']
},
'userID': 1,
'price': '10.00',
'condition': 'New',
'image1': '',
'image2': '',
'image3': '',
'image4': '',
'image5': '',
'comment': ''
}
然后我想得到这本书的标题,所以下面是我的代码:
{% for key, value in book.items %}
{% if value.title %}
<h3> {{value.title}}</h3>
{% endif %}
{% endfor %}
但是,我不仅获得了标题,还获得了打印的价格和条件。有人可以让我知道我做错了什么吗?
解决方案
推荐阅读
- css - 未在前端应用 CKEEditor 5 CSS
- android - 如何解决创建签名 APK 文件的问题?
- java - 如何在不同的计算机上打开导出的 .exe Jar 文件或 RUN.bat 文件?
- javascript - 为输入类型文件和材质 ui 按钮正确参数化事件处理程序
- c# - 过滤。(此文件夹)和 ..(父文件夹)在 C# SSH.NET 中的 SFTP 目录列表中
- python - 从 Python 中的 imagemagick 子进程读取输出
- python - 在 python 3.6 Mac OSX 10.12 中打开多个 IDLE shell
- sql - 如何在 SQL 中查找 int 值的 MIN 和 MAX 作为子字符串?
- javascript - 如何以 JSON 格式输出数据?
- java - 递归目录树时计算目录大小