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 %}
标签:name,gender,django,语法,item,role,key,n3,模板
From: https://www.cnblogs.com/CarlosX/p/16949266.html