django前端制表
时间: 2023-06-28 20:13:31 浏览: 42
在 Django 中可以使用模板语言来进行前端制表,下面是一个简单的例子:
1. 首先在 views.py 中定义数据:
```python
from django.shortcuts import render
def table_view(request):
data = [
{'name': 'John', 'age': 25, 'gender': 'Male'},
{'name': 'Mary', 'age': 30, 'gender': 'Female'},
{'name': 'Bob', 'age': 35, 'gender': 'Male'},
]
return render(request, 'table.html', {'data': data})
```
2. 在 templates 文件夹中新建 table.html 文件:
```html
<!DOCTYPE html>
<html>
<head>
<title>Table View</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
<td>{{ item.gender }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
```
在模板中使用了 for 循环来遍历数据并输出到表格中。可以根据实际需求来修改表格样式和数据。