过滤器
常见的过滤器
过滤器应用举例
模版
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{# 如果该变量值为None,默认给定一个值#}
<p>{{student|default_if_none:'no student'}}</p>
<hr>
{# 如果该变量不存在,默认给定一个值 #}
<p>{{age|default:"this value isn't exist"}}</p>
<hr>
{{dtime|date:"y-m-d H:i:s"}}
<hr>
{# 取消转义 #}
{% autoescape off %}
{{word}}
{% endautoescape %}
<hr>
{{join_word|join:'-'}}
</body>
</html>
路由
urlpatterns = [
# 过滤器
path('filter/',views.filter_test,name='filter'),
]
视图
def filter_test(request):
student = None
dtime = datetime.now()
word = "<p>说点儿什么好呢</p>"
join_word = [1,2,3]
return render(request,'filter_test.html',locals())
页面