使用模板
- 在myapp文件中,新建目录tamplates
- 在tamplates目录中新建模板文件test.html
- 编辑test.html,添加一下代码:
<!DOCTYPE html>
<html>
<head>
<title>My First Template</title>
</head>
<body>
<h1>欢迎</h1>
</body>
</html>
- 编辑views.py,添加以下内容
# -*- coding:utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, 'test.html')
- 编辑urls.py,添加一下内容
from django.urls import path
from myapp import views # 导入views模块
urlpatterns = [
path('', views.index), # 将url映射成模板文件
]
- 运行django服务器,查看模板是否成功渲染(如果与下图相同则渲染成功):
模板的用法:
可以参考该教程:http://djangobook.py3k.cn/2.0/chapter04/
使用模板继承的时候,{% extend ” %}一定是子模板中的第一个标签,把该语句放到子模板文件的第一行就行