django學習筆記-模板語法
阿新 • • 發佈:2022-12-04
1. 引數傳遞
name = "張三" alist = ['a', 'b', 'c'] adict = {"name": "張三", "role": "管理員", "gender": "男"} data_list = [ {"name": "張三", "role": "管理員", "gender": "男"}, {"name": "李四", "role": "成員", "gender": "男"}, {"name": "王五", "role": "成員", "gender": "男"} ] return render( request, "test1.html", {"n1": name, "n2": alist, "n3": adict, "n4": data_list} )
2. 模板語法
2.1 普通引數
<div>{{ n1 }}</div>
2.2 列表引數
<div>{{ n2 }}</div>
<div>{{ n2.0 }}</div>
<div>{{ n2.1 }}</div>
<div>{{ n2.2 }}</div>
{% for item in n2 %}
<span>{{ item }}</span>
{% endfor %}
2.3 字典引數
<div>{{ n3 }}</div> <div>{{ n3.name }}</div> <div>{{ n3.role }}</div> <div>{{ n3.gender }}</div> <ul> {% for key,value in n3.items %} <li>{{ key }}:{{ value }}</li> {% endfor %} </ul>
2.4 列表字典混合
<table> <thead> <tr> {% for key in n4.0.keys %} <th>{{ key }}</th> {% endfor %} </tr> </thead> <tbody> {% for item in n4 %} <tr> <td>{{ item.name }}</td> <td>{{ item.role }}</td> <td>{{ item.gender }}</td> </tr> {% endfor %} </tbody> </table>
2.5 if判斷
{% if n1 == "a" %}
<div>a</div>
{% elif n1 == "b" %}
<div>b</div>
{% else %}
<div>c</div>
{% endif %}