def tmp_demo(request):
context = {
'var':'iloveyou',
'arr':['aa','bb','cc'],
'user':{'username':'admin','email':'admin@qq.com'},
'con':'<h1 style="color:red;">iloveyou</h1>',
'num':12
}
return render(request,'tmp/1.html',context)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>模板文件</title>
</head>
<body>
{# 加载模块,并以标签内的参数进行渲染 #}
{% include 'tmp/header.html' %}
<div style="min-height: 400px;">
<ul>
<li> {{ var|default:'无' }}</li> //接收值时如果参数为空,那么设置默认值为无
<li>输出变量:{{ var }}</li> {# 返回值为: iloveyou #}
<li>输出列表:{{ arr }}</li> {# 返回值为: ['aa','bb','cc'] #}
<li>以下标输出列表中的某个值:{{ arr.2 }}</li>{# 返回值为: cc #}
<li>输出字典:{{ user }}</li> {# 返回值为: {'username':'admin','email':'admin@qq.com'} #}
<li>输出字典中值:{{ user.username }}</li> {# 返回值为: admin #}
{# <li>输出字典中值:{{ user['username'] }}</li> #}
<li>
模板引擎的标签 {% url 'tmp_demo' %} {# 执行url name = 'tmp_demo'的函数 #}
</li>
{% comment %}
多行注释
{% endcomment %}
</ul>
</div>
{# 加载模块,并以标签内的参数进行渲染 #}
{% include 'tmp/footer.html' %}
</body>
</html>