Python 循环不向表中添加数据
for循环可以工作,但它不是将数据添加到表中,而是将数据添加到现有表下 laptop_list.htmlPython 循环不向表中添加数据,python,django,Python,Django,for循环可以工作,但它不是将数据添加到表中,而是将数据添加到现有表下 laptop_list.html <div class="container"> <div class="table-responsive-sm"> <table class="table"> {%for laptop in laptops%} <div class="table-responsive-sm"> <
<div class="container">
<div class="table-responsive-sm">
<table class="table">
{%for laptop in laptops%}
<div class="table-responsive-sm">
<tbody>
<tr>
<td>{{laptop.laptop_id}}</td>
<td>{{laptop.make}}</td>
<td>{{laptop.model}}</td>
<td>{{laptop.specification}}</td>
<td>{{laptop.warranty}}</td>
<td>{{laptop.condition}}</td>
<td>{{laptop.price}}</td>
</tr>
</tbody>
</div>
</table>
{%endfor%}
</div>
</div>
views.py
def laptop_list(request):
laptops = Laptop.objects.all()
return render(request,'laptops/laptop_list.html',{'laptops':laptops})
这里有几个错误 不能将
div
作为表元素的子元素。您应该删除表中重复的表响应sm
div
此外,该表应该只有一个t正文
元素。t车斗应位于for回路外部
因此:
{%用于笔记本电脑中的笔记本电脑%}
{{laptop.laptop_id}
{{laptop.make}
{{laptop.model}
{{laptop.specification}}
{{笔记本电脑.保修}
{{laptop.condition}}
{{laptop.price}}
{%endfor%}
啊,谢谢。我想,是那些小家伙把你迷住了。
def laptop_list(request):
laptops = Laptop.objects.all()
return render(request,'laptops/laptop_list.html',{'laptops':laptops})
<div class="container">
<div class="table-responsive-sm">
<table class="table">
<tbody>
{% for laptop in laptops %}
<tr>
<td>{{laptop.laptop_id}}</td>
<td>{{laptop.make}}</td>
<td>{{laptop.model}}</td>
<td>{{laptop.specification}}</td>
<td>{{laptop.warranty}}</td>
<td>{{laptop.condition}}</td>
<td>{{laptop.price}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>