Django--模板語法
阿新 • • 發佈:2018-11-09
模版語法之變數,深度查詢
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>
模版語法之過濾器