第一种:接收普通的基本数据类型
接收基本数据类型,views.py中通过赋值的方式传送给模板文件,例子如下:
模板文件index.html代码如下
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{
{
title}}</title>
</head>
<body>
<h1>hello ,world</h1>
<h1>hello,{
{
name}}</h1><!--接收基本数据类型-->
</body>
</html>
views.py代码如下
from django.http import HttpResponse
from django.template import loader,Context
def index(request):
t=loader.get_template("index.html")#加载模板文件
#这里的"title"和"name"对应着index.html文件中的title和name,
c=Context({
"title":"firstPage","name":"wuranghao"})#通过赋值的方式传送给模板文件
return HttpResponse(t.render(c))
第二种:模板文件接受字典数据类型,可以通过key得到value
当模板文件接受到字典数据类型时,可以通过字典数据类型的key来访问value。
例子如下:
其中index.html文件的内容如下:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{
{
title}}