1、模板全局配置
TEMPLATES = [
{
\'BACKEND\': \'django.template.backends.django.DjangoTemplates\',
\'DIRS\': [os.path.join(BASE_DIR, \'templates\')]
,
\'APP_DIRS\': True,
\'OPTIONS\': {
\'context_processors\': [
\'django.template.context_processors.debug\',
\'django.template.context_processors.request\',
\'django.contrib.auth.context_processors.auth\',
\'django.contrib.messages.context_processors.messages\',
],
},
},
]
2、视图中添加返回的HTML页面
from django.shortcuts import render
模板渲染类
class IndexView(View):
\'\'\'
index 类视图
\'\'\'
# 调用get请求
def get(self,request):
datas = [
{
\'project_name\':\'宁波交检\',
\'leader\':\'臧宏伟\',
\'app_name\':\'获奖的项目\'
},
{
\'project_name\':\'宁波交检\',
\'leader\':\'臧宏伟\',
\'app_name\':\'获奖的项目\'
},
{
\'project_name\':\'宁波交检\',
\'leader\':\'臧宏伟\',
\'app_name\':\'获奖的项目\'
}
]
# request:前端接受到的所有数据
#\'index.html\':模板名称
# locals():函数会以字典类型返回当前位置的全部局部变量
return render(request,\'index.html\',locals())
3、将返回的数据插入到HTML页面中
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">序号</th>
<th scope="col">项目名称</th>
<th scope="col">项目负责人</th>
<th scope="col">应用名称</th>
</tr>
</thead>
<tbody>
{# 模板#}
{# for 循环 便利所有接受到的数据#}
{% for one in datas %}
<p>这是后端造的孽</p>
<tr>
<th scope="row">{{ forloop.counter }}</th>
<td>{{ one.project_name }}</td>
<td>{{ one.leader }}</td>
<td>{{ one.app_name }}</td>
</tr>
{% endfor %}
</tbody>
</table>