【发布时间】:2020-01-02 01:11:48
【问题描述】:
我有以下型号
class Table(models.Model):
# Some not important attrs
rows = IntegerField(etc)
cols = IntegerField(etc)
然后我就有了我在哪里渲染这个模型的对象的视图。我需要根据每个对象的行数和列数构建一些 HTML 表格。
查看:
get_tables(request):
tables = Table.objects.all()
return render(request, 'tables.html', {'tables': tables})
我正在尝试做类似的事情:
{% for table in tables %}
<table>
<thead>
<tr>
{% for col in table.cols %}
<th>column label here</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in table.rows %}
<tr>my row</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}
我知道可以循环输入字典中的键。但是值 cols 和 rows 是整数。如何在 Django 模板上实现这一点?
【问题讨论】:
标签: django loops templates integer iteration