这一部分主要涉及到后台View的设计,前端HTML页面的设计:
后台设计View:
from django.views.generic import View
class ForTrView(View):
def get(self, request):
integer_list = []
string_list = []
# 这里设计两个列表,一会前端填充这两个列表
for i in range(10):
integer_list.append(i)
string_list.append("string:" + str(i))
return render(request, 'for_tr.html', {
# 方法一:(推荐)
"item_method": [{'item1': t[0], 'item2': t[1]} for t in zip(integer_list, string_list)],
# 方法二:
'zip_method': zip(integer_list, string_list),
})
前端设计HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>upload_table</title>
</head>
<body>
<table>
<h1>方法1:</h1>
{% for item_loop in item_method %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ item_loop.item1 }}</td>
<td>{{ item_loop.item2 }}</td>
</tr>
{% endfor %}
</table>
<table>
<h1>方法2:</h1>
{% for integer,string in zip_method %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ integer }}</td>
<td>{{ string }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
然后就可以得到如下结果:
方法1:
1 0 string:0
2 1 string:1
3 2 string:2
4 3 string:3
5 4 string:4
6 5 string:5
7 6 string:6
8 7 string:7
9 8 string:8
10 9 string:9
方法2:
1 0 string:0
2 1 string:1
3 2 string:2
4 3 string:3
5 4 string:4
6 5 string:5
7 6 string:6
8 7 string:7
9 8 string:8
10 9 string:9