django框架学习之8:模板过滤器

所谓过滤器就是指对模板中的某些内容进行相关处理和过滤,要有模板过滤器先要有模板,

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值