第二十七节 过滤器一(add和cut)

add 过滤器:加法或者字符串拼接,列表拼接

views.py 代码

1 from django.shortcuts import render,HttpResponse,redirect,reverse
2 
3 def add_view(request):
4     context = {
5         'value1':[1, 2, 3, 4],
6         'value2':['a', 'b', 'c', 'd']
7     }
8     return render(request, 'index.html', context=context)

index.html 代码

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>模板渲染</title>
 6 </head>
 7 <body>
 8     {{ '1'|add:'2' }}
 9     <!-- 输出3  -->
10     <br>
11     {{ value1|add:value2 }}
12     <!-- 输出 [1, 2, 3, 4, 'a', 'b', 'c', 'd'] -->
13 </body>
14 </html>

cut 过滤器:剪切掉指定字符串

views.py 代码

1 from django.shortcuts import render,HttpResponse,redirect,reverse
2 
3 def cut_view(request):
4     return render(request, 'index.html')

index.html 代码

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>模板渲染</title>
 6 </head>
 7 <body>
 8     {{ '1234'|cut:'2' }}
 9     <!-- 输出 134 -->
10 </body>
11 </html>
原文地址:https://www.cnblogs.com/kogmaw/p/12448175.html