【发布时间】:2021-09-19 12:22:50
【问题描述】:
我有一个由通用 ListView 引用的模型,并提供给模板。尝试在模板中创建表格给我一个TypeError: not iterable - 我做错了什么?
示例代码
Class bookmodel(models.Model):
Book = models.CharField(max_length=255)
Author = models.CharField(max_length=255)
观看次数
Class bookview(generic.ListView):
model = bookmodel
template = “books.html”
生成object_list 类似:
<Queryset [<bookmodel: Grapes of Wrath >, <bookmodel: I, Robot>]>
模板布局如下:
{% extends ‘base.html’ %}
{% block content %}
<table>
<thead>
<tr>
<th> book </th>
<th> author </th>
</tr>
</thead>
<tbody>
{% for object in object_list %}
<tr>
{% for field in object %}
<td> {{ field }} </td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
但这会因上述错误而失败。
【问题讨论】:
-
如果可行,请尝试发布答案。
标签: django django-templates django-queryset