模板语法
1.深度查询:
def index(request): name = "root" age = 13 sex = True lve = ["swimming","shopping","coding",] bookinfo = {"id":10,"price":9.0} return render(request,"index.html",locals())
//locals()代表传入的所有变量
#模板代码template/index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <a> hi {{name}}</a> //普通变量取值 <a>lve :{{lve.1}}</a>//通过局点符取出列表中的第二个元素 <a>lve :{{bookinfo.id}}</a> //通过句点符取出字典中id的 值 </body> </html>
//句点符表示深度查询
2.嵌套,继承
{% include "模版文件名" %}#模版嵌入
{% extends "base.html" %}#模板继承
def index(request):
""" 模版语法"""
return render(request,"index.html",locals())
#子模版部分 templates/index.html {% extends "base.html"%} ......内容 {{block.super}}#继承父盒子中的默认数据 {% endblock %} #必须放在最上面 #父模版 templates/base.html .......内容 [% block title %] <title>title<tetle/> {% endblock %} ............内容

浙公网安备 33010602011771号