数据信息展示
# 知识点:
# 1. 获取数据库所有内容
list = PersonInfo.objects.all()
# 2. html中的 for 循环
{% for ls in list %}
<tr>
<td>{{ ls.id }}</td>
<td>{{ ls.name }}</td>
<td>{{ ls.sex }}</td>
</tr>
{% endfor %}
流程
# 1. 创建视图函数(views.py)
def shou_list(request):
# 调用model类查询所有数据
list = PersonInfo.objects.all()
print(list) # 获取的是对象
# 返回一个http模板
return render(request, template_name="book/shou_list.html", context={"list":list})
# 2. 绑定路由(book.urls.py)
re_path(r'^shou_list/$',shou_list),
# 3. 编辑html页面(templates.book.show_list.html)
<table width="60%" border="1" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>编号</td>
<td>姓名</td>
<td>性别</td>
</tr>
{% for ls in list %}
<tr>
<td>{{ ls.id }}</td>
<td>{{ ls.name }}</td>
<td>{{ ls.sex }}</td>
</tr>
{% endfor %}
</table>
# 4. 浏览器访问
http://127.0.0.1:8000/shou_list/
标签:shou,展示,08,list,book,html,ls,数据库,name
From: https://www.cnblogs.com/kh-1314/p/17085555.html