路由
urlpatterns = [
...
url(r'^student/', include('stu.urls')),
]
子路由
stu/urls.py
urlpatterns = [
...
url(r'^show/$', views.show_view)
]
视图
stu/views.py
def show_view(request):
# 查询表中所有数据
stus = Student.objects.all()
# 返回页面 字典类传递学生信息
return render(request, 'show.html', {'students': stus})
页面
templates/show.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学生列表</title>
</head>
<body>
<table border="1" cellspacing="0">
<tr>
<th>编号</th>
<th>姓名</th>
<th>密码</th>
</tr>
{% for stu in students %}
<tr>
<td>{{ stu.id }}</td>
<td>{{ stu.sname }}</td>
<td>{{ stu.spwd }}</td>
<tr>
{% endfor %}
</table>
</body>
</html>
如果编号不适用id采用计数的方式
{# <td>{{ stu.id }}</td>#}
<td>{{ forloop.counter }}</td>
模板语法有点类似于java的themeleaf
运行,访问:
http://127.0.0.1:8000/student/show/
网友评论