html - 在 HTML 表格中显示上下文变量
问题描述
我想要做的是用与我的字典中的条目相同的行数填充一个 html 表。我将我的字典项作为上下文变量传递给 html。我可以按如下方式显示我的字典项目:
{% for key,value in top_items.items %}
<ul>{{ key }}</ul>
{% endfor %}
但是当我尝试将其粘贴到表格中以创建表格行时,如下所示,它不起作用。似乎将它们全部排成一排。而不是为每个项目创建一个新行。
<table class="u-half-width">
<thead>
<tr>
<th>Column</th>
<th>Details</th>
</tr>
</thead>
<tbody>
<tr>
{% for key,value in top_items.items %}
<tr>{{ key }}</tr>
{% endfor %}
</tr>
</tbody>
</table>
解决方案
您的 HTML 无效;您不能将 a 嵌套<tr>
在 another<tr>
中,并且您没有任何<td>
/<th>
元素。
尝试:
<tbody>
{% for key,value in top_items.items %}
<tr>
<th scope="row">{{ key }}</th>
<td>{{ value }}</td>
</tr>
{% endfor %}
</tbody>
推荐阅读
- python - 如何使用此代码仅获取我想移动到新数据框的列?
- r - 有没有办法将多个excel文件读入R,但只能到某个创建日期?(注意:实际的 excel 文件中不存在日期。)
- javascript - 每次我调用它的树枝模板时执行 javascript
- r - R - 满足条件时替换多个字段中的值
- angular - 如何在 Angular 应用程序中添加 ACE Editor 荧光笔规则?
- swift - 在空的 UITextField Swift 工作示例中检测退格
- excel - 为什么宏单独工作,当你将它与另一个宏合并时不起作用?
- r - Shinyapps.io:未加载包
- json - 具有 VPC、子网和安全组选择的 EC2 的 CloudFormation 模板 (JSON)
- machine-learning - AWS Sagemaker BlazingText 多个训练文件