所谓过滤器就是指对模板中的某些内容进行相关处理和过滤,要有模板过滤器先要有模板,
def news(request):
context = {
'person':['cheney','jerry'],
'net': '<a href="https://www.baidu.com">百度</a>',
'num': '5',
'now':datetime.now(),
#'info':1234
'str1': 'cheney12345',
}
return render(request,'news.html',context=context)
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--<a href="/news/">进入新闻主页</a>-->
<a href= "{% url 'index' %}" >进入新闻主页</a>
<a href= "{% url 'news_detail' 1 3 %}?name=cheney" >进入新闻详情页</a>
<a href= "{% url 'news_detail' news_id=2 type_id=4 %}?name=cheney" >进入新闻2详情页</a>
<p>
{% with name=person.0 %}
{{ name }}
{% endwith %}
{% with person.1 as name%}
{{ name }}
{% endwith %}
{{ name }}
</p>
<!--<p>-->
<!-- {{ net }}-->
<!-- {% autoescape off %}-->
<!-- {{ net }}-->
<!-- {% endautoescape %}-->
<!--</p>-->
<p> {{ num|add:2 }} </p>
<p> {{ num|add:'2' }} </p>
<p>{{ num|add:'abc' }}</p>
<p>{{ 'hello world'|cut:'o' }}</p>
<p>{{ now|date:'Y/m/d:H:i:s' }}</p>
<p>{{ info|default:'这个人很懒,什么都没有留下' }}</p>
<ul>
<li>{{ 34.32|floatformat }}</li> 34.3
<li>{{ 34.35|floatformat }}</li> 34.4
<li>{{ 34.353333|floatformat:3}}</li> 34.353
</ul>
<p>{{ net|safe }}</p>
<p>{{ net|striptags }}</p>
<p>{{ str1|truncatechars:6 }}</p>
</body>
</html>