模版语法之变量,深度查询
def index(request): # 常用变量 name = 'lxx' age = 18 newage = 18.5 lis = [1,2,3] dic = { "name":'yxx','sex':'man'} tup = (1,"zz",4,) # 函数 def func1(): print("xx") return 'func1' class test(): def __init__(self,name,age): self.name = name self.age = age def obj_func(self): return self.age @classmethod def cla_func(cls): return 'cls_func' @staticmethod def stati_test(): return 'static' obj = test('nxx',18) obj2 = test('zxx',58) obj_lis = [obj,obj2] return render(request,'index.html',locals())
html文件
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>模板渲染</title> </head> <body>
模板语言变量
<p>{{ name }}</p> <p>{{ age }}</p> <p>{{ newage }}</p> <p>{{ lis }}</p> <p>{{ tup }}</p> <p>{{ dic }}</p> <p>{{ func1 }}</p> 深度查询 <p>{{ obj }}</p> <p>{{ obj.name }}</p> <p>{{ obj.obj_func }}</p> <p>{{ obj.cla_func }}</p> <p>{{ obj.stati_test }}</p> <p>{{ obj_lis.0.name }}</p> </body> </html>