我想使用嵌套的 for 循环遍历我模型的所有项目,而且还通过选择我在这个模型中决定的字段!
想法是在数据表中显示所有项目,但只显示我选择的字段。
这是我尝试过的:
<table class="table table-bordered" id="example" style="text-align: center; font-size: 14px;">
<thead class="table-success">
<tr>
{% for verb in verbose %}
<th>{{ verb }}</th>
{% endfor %}
</tr>
</thead>
<tr>
{% for test in tests %}
{% for f in fields %}
<td class="body-table" style="padding: 1px;">{{test}}.{{f}} </td>
{% endfor %}
{% endfor %}
</tr>
</table>
我让我认为可以自我解释的错误
{{test}}.{{f}}
{% for test in tests %}
<tr>
{% for f in fields %}
<td class="body-table" style="padding: 1px;">{{ getattr(test, f) }}</td>
{% endfor %}
</tr>
{% endfor %}
您可以使用
getattr()
访问模型实例。