1.在项目目录下新建一个templates目录,在templates目录下新建一个booktest目录,再新建一个index.html
然后在setting.py中设置模板的路径
如图所示:

2.定义模板
打开templtes/booktest/index.html文件,定义代码如下:
<html>
<head>
<title>图书列表</title>
</head>
<body>
<h1>{{title}}</h1>
{%for i in list%}
{{i}}<br>
{%endfor%}
</body>
</html>
3.打开booktst/views.py文件,写以下代码:
from django.shortcuts import render
def index(request):
context={'title':'图书列表','list':range(10)}
return render(request,'booktest/index.html',context)
4.效果

网友评论