django之创建第4个项目编写第一个动态模板文件

时间:2023-03-09 21:28:58
django之创建第4个项目编写第一个动态模板文件

修改的地方:

1、index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>django之创建第四个项目</title>
</head>
<body>
<h1>hello,{{name}}</h1> <!--模板变量用变量名定义,如:name-->
</body>
</html>

2、修改views.py文件

# Create your views here.
#coding:utf-8
from django.http import HttpResponse #导入templates文件所需导入库
from django.template import loader,Context def index(request):
#加载器,加载模板
t=loader.get_template("index.html") #动态,传入了变量
c=Context({"name":"Django"})
return HttpResponse(t.render(c))