文章目录
视图
项目原型设计时的主页:
【1】views.py
from boards.models import Board
def home(request):
"""在主页列出所有板块"""
boards = Board.objects.all() # 通过模型管理器获取所有的板块
return render(request, 'home.html', {'boards': boards})
字典:代表要添加的内容
【2】模板引擎
- 在manage.py同级目录下创建templates文件夹,新建home.html
<!Dpctype html>
<html>
<head>
<meta charset='utf-8'>
<title>Boards</title>
</head>
<body>
<h1>Boards</h1>
<table border="1">
<thead>
<tr>
<th>Board</th>
<th>Posts</th>
<th>Topics</th>
<th>Last Post</th>
</tr>
</thead>
<tbody>
{% for board in boards %}
<tr>
<td>
{{ board.name }}<br>
<small style="color: #888">{{ board.description}}</small>
</td>
<td>0</td>
<td>0</td>
<td></td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>