目录
django拥有一套完整的模板系统
1.变量 {{ }}
(1)传入前端的变量最外层一定是一个字典
(2)具体的变量可以是字符串,列表,字典类型
(3)变量通过.方法可以调用自己的无参数方法,索引
(4)如果变量是字典,也可以通过键调用值
views.py
from django.http import HttpResponse
from django.template import Template,Context
def variable(request):
template=Template(
"""
<p>变量常规用法:{{variable}}</p>
<p>变量常规用法_变量列表:{{variable1}}</p>
<p>变量常规用法_变量索引:{{variable1.1}}</p>
<p>变量常规用法_变量无参数方法:{{variable2.upper}}</p>
<p>变量常规用法_变量字典:{{variable3}}</p>
<p>变量常规用法_变量字典键值:{{variable3.name}}</p>
"""
)
data=Context({
"variable":"张三",
"variable1":[1,2,3,4],
"variable2":"zhangsan",
"variable3":{"name":"李四","gender":"男"},
})
result=template.render(data)
return HttpResponse(result)
urls.py

服务器运行:

2.标签 {% %}
(1){% if %}
判断指定条件true,false ,通常判断一个变量是否存在或者为真
{% if %}标签需要{% endif %}标签进行闭合
{% if %}可以有{% else %}
视图文件 views.py
from django.http import HttpResponse
from django.template import Template,Context
def label_if(request,name):
"""
if标签
"""
template=Template(
"""
{% if username %}
<p>欢迎登陆 {{username}}</p>
{% else %}
<a href="#">请登录</a>
{% endif %}
"""
)
data=Context({
"username":name,
})
result=template.render(data)
return HttpResponse(result)
路由文件 urls.py



(2){% ifequal %}
判断两个量是否相等,通常用于比较
{% ifequal %}标签需要{% endifequal %}标签进行闭合
{% ifequal %}可以有{% else %}
视图文件 views.py
from django.http import HttpResponse
from django.template import Template, Context
def label_ifequal(request, name, gender):
"""
ifequal标签
"""
template = Template(
"""
{% ifequal gender "M" %}
<p>{{username}}先生,你好!</p>
{% else %}
<p>{{username}}女士,你好!</p>
{% endifequal %}
"""
)
data = Context({
"username": name,
"gender": gender,
})
result = template.render(data)
return HttpResponse(result)
路由文件 urls.py
re_path(r'label_ifequal/(?P<name>\w{1,4})/(?P<gender>[M,F])',label_ifequal),


(3){% for %}
{% for %}标签需要{% endfor %}标签进行闭合
{% for %}标签采用了和python一样的for in结构
{% for %}循环出来的不是html对象,只是数据
视图文件 views.py
from django.http import HttpResponse
from django.template import Template,Context
def label_for(request):
"""
for标签
"""
template=Template(
"""
<table>
<th>标题</th>
<th>作者</th>
<th>类型</th>
<th>封面</th>
{% for article in articles %}
<tr>
<td>{{article.title}}</td>
<td>{{article.author}}</td>
<td>{{article.type}}</td>
<td><img style='width:100px;height:100px;' src='{{article.picture}}'></td>
</tr>
{% endfor %}
</table>
"""
)
data=Context({
"articles":[
{
"title": "背影",
"author": "朱自清",
"type": "散文",
"picture": "https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1553755201&di=e10ae2618dce21703114e119402a522e&src=http://img3.duitang.com/uploads/item/201507/05/20150705135641_K4sVA.jpeg"
},
{"title": "诛仙", "author": "萧鼎", "type": "小说",
"picture": "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=328549116,1379468112&fm=26&gp=0.jpg"},
{"title": "西游记", "author": "吴承恩", "type": "小说",
"picture": "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4100856954,312579948&fm=26&gp=0.jpg"},
{"title": "鲁宾逊漂流记", "author": "丹尼尔·笛福", "type": "小说",
"picture": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553750785199&di=d906d54cf8456736d285b99f601bb968&imgtype=jpg&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D1665326279%2C2802541090%26fm%3D214%26gp%3D0.jpg"},
{"title": "活着", "author": "余华", "type": "小说",
"picture": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553750868172&di=ccae9a343204f59cecbcc21c9e811e58&imgtype=0&src=http%3A%2F%2Fs1.sinaimg.cn%2Flarge%2F001Pe7mxzy76VARpXvdc3"},
]
})
result=template.render(data)
return HttpResponse(result)
路由文件 urls.py


在这讲一下{{forloop}}变量:
forloop是django模板语言定义好的一个变量,用来表示循环当中的每次循环
forloop为嵌套循环提供了parentloop参数来记录当前玄幻的上一层循环的情况
通常我们使用forloop标签进行循环的口头或者末尾判断
视图文件 views.py
from django.http import HttpResponse
from django.template import Template,Context
def label_forloop(request):
"""
forloop 变量
"""
template=Template(
"""
<h3>单层循环</h3>
<ul>
{% for i in range %}
<p>{{forloop}}</p>
{% endfor %}
</ul>
<h3>嵌套循环</h3>
<ul>
{% for i in range %}
{% for article in articles %}
<li>{{article}}是第{{forloop.counter}}次循环,它的父循环是第{{forloop.parentloop.counter}}次循环</li>
{% endfor %}
{% if not forloop.last %}
<p>=================================</p>
{% endif %}
{% endfor %}
</ul>
"""
)
data=Context({
"articles":["背影","西游记","水浒传","三国演义","活着"],
"range":["a","b","c"],
})
result=template.render(data)
return HttpResponse(result)
路由文件 urls.py


3.过滤器 filter {{ | }}
对变量进行具体功能的处理
safe:去掉前端的转义
upper:大写
视图文件 views.py
from django.http import HttpResponse
from django.template import Template, Context
def filter(request, str):
template = Template(
"""
{{argv|safe}}
<br>
{{str|upper}}
"""
)
data = Context({
"argv": "<img style='width:100px;height;100px;' src='http://cdn.duitang.com/uploads/item/201507/15/20150715122359_WmwTv.jpeg'>",
"str": str,
})
result = template.render(data)
return HttpResponse(result)
路由文件 urls.py


179



被折叠的 条评论
为什么被折叠?



