通过管道符 "|" 来使用过滤器,{{ value|过滤器:参数 }}
Django 的模板语言中提供了六十个左右的内置过滤器
urls.py:
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^filter_test/', views.filter_test), ]
如果一个变量的变量名不存在或者没有给其传值,可以通过 default 使用默认值
views.py:
from django.shortcuts import render, redirect, HttpResponse from app01 import models # Filter 测试 def filter_test(request): name = "Jack" age = 18 return render( request, "filter_test.html", { "name": name, "age": None, })
这里的 age 变量传了一个空值
filter_test.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Filter 测试</title> </head> <body> <h1>Filter 测试页面</h1> <!-- default --> {{ name }} <br> {{ name1|default:"这里不存在" }} <br> {{ age|default:"这里也不存在" }} </body> </html>
name1 是没有定义的变量
运行效果:
注意:
这里不能带有空格
length:
返回字符串或列表的长度
views.py:
from django.shortcuts import render, redirect, HttpResponse from app01 import models # Filter 测试 def filter_test(request): name = "Jack" age = 18 hobby = ["Reading", "Basketball", "Movie", "Music"] return render( request, "filter_test.html", { "name": name, "age": None, "hobby_list": hobby, })
filter_test.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Filter 测试</title> </head> <body> <h1>Filter 测试页面</h1> <!-- default --> {{ name }} <br> {{ name1|default:"这里不存在" }} <br> {{ age|default:"这里也不存在" }} <br> <!-- length --> {{ name|length }} <br> {{ hobby_list|length }} <br> {{ age|length }} </body> </html>
运行效果:
这里计算了 name 变量值、hobby_list 列表、age 空值的长度
slice:
slice 是用来进行切片的
views.py:
from django.shortcuts import render, redirect, HttpResponse from app01 import models # Filter 测试 def filter_test(request): name = "Jack" age = 18 hobby = ["Reading", "Basketball", "Movie", "Music"] return render( request, "filter_test.html", { "name": name, "age": None, "hobby_list": hobby, })
filter_test.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Filter 测试</title> </head> <body> <h1>Filter 测试页面</h1> <!-- default --> {{ name }} <br> {{ name1|default:"这里不存在" }} <br> {{ age|default:"这里也不存在" }} <br> <!-- length --> {{ name|length }} <br> {{ hobby_list|length }} <br> {{ age|length }} <br> <!-- 切片 --> {{ hobby_list|slice:"0:2" }} <br> {{ name|slice:"0:2" }} </body> </html>
运行效果:
filesizeformat:
将一串数字格式化成文件格式
views.py:
from django.shortcuts import render, redirect, HttpResponse from app01 import models # Filter 测试 def filter_test(request): name = "Jack" age = 18 file_size = 988222836293733 hobby = ["Reading", "Basketball", "Movie", "Music"] return render( request, "filter_test.html", { "name": name, "age": None, "hobby_list": hobby, "filesize": file_size, })
filter_test.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Filter 测试</title> </head> <body> <h1>Filter 测试页面</h1> <!-- default --> {{ name }} <br> {{ name1|default:"这里不存在" }} <br> {{ age|default:"这里也不存在" }} <br> <!-- length --> {{ name|length }} <br> {{ hobby_list|length }} <br> {{ age|length }} <br> <!-- 切片 --> {{ hobby_list|slice:"0:2" }} <br> {{ name|slice:"0:2" }} <br> <!-- filesizeformat --> {{ filesize|filesizeformat }} </body> </html>
运行结果:
这里会根据大小格式化成 KB,MB,GB 和 TB
cut:
删除字符串或列表中和过滤器参数相同的内容
views.py:
from django.shortcuts import render, redirect, HttpResponse from app01 import models # Filter 测试 def filter_test(request): name = "Jack" age = 18 file_size = 988222836293733 hobby = ["Reading", "Basketball", "Movie", "Music"] return render( request, "filter_test.html", { "name": name, "age": None, "hobby_list": hobby, "filesize": file_size, })
filter_test.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Filter 测试</title> </head> <body> <h1>Filter 测试页面</h1> <!-- default --> {{ name }} <br> {{ name1|default:"这里不存在" }} <br> {{ age|default:"这里也不存在" }} <br> <!-- length --> {{ name|length }} <br> {{ hobby_list|length }} <br> {{ age|length }} <br> <!-- 切片 --> {{ hobby_list|slice:"0:2" }} <br> {{ name|slice:"0:2" }} <br> <!-- filesizeformat --> {{ filesize|filesizeformat }} <br> <!-- cut --> {{ name|cut:"ac" }} <br> {{ hobby_list|cut:"Movie" }} </body> </html>
运行结果:
Jack 被删除了 ac,hobby_list 列表被删除了 Movie
join:
连接字符串
views.py:
from django.shortcuts import render, redirect, HttpResponse from app01 import models # Filter 测试 def filter_test(request): name = "Jack" age = 18 file_size = 988222836293733 hobby = ["Reading", "Basketball", "Movie", "Music"] return render( request, "filter_test.html", { "name": name, "age": None, "hobby_list": hobby, "filesize": file_size, })
filter_test.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Filter 测试</title> </head> <body> <h1>Filter 测试页面</h1> <!-- default --> {{ name }} <br> {{ name1|default:"这里不存在" }} <br> {{ age|default:"这里也不存在" }} <br> <!-- length --> {{ name|length }} <br> {{ hobby_list|length }} <br> {{ age|length }} <br> <!-- 切片 --> {{ hobby_list|slice:"0:2" }} <br> {{ name|slice:"0:2" }} <br> <!-- filesizeformat --> {{ filesize|filesizeformat }} <br> <!-- cut --> {{ name|cut:"ac" }} <br> {{ hobby_list|cut:"Movie" }} <br> <!-- join --> {{ name|join:"+" }} <br> {{ hobby_list|join:"-" }} </body> </html>
运行结果:
safe:
Django 模板会自动对 html、js 标签进行转义,safe 可以让 Django 不对标签进行转义
views.py:
from django.shortcuts import render, redirect, HttpResponse from app01 import models # Filter 测试 def filter_test(request): name = "Jack" age = 18 file_size = 988222836293733 test_html = "<a href='www.baidu.com'>这是一个 html 标签</a>" hobby = ["Reading", "Basketball", "Movie", "Music"] return render( request, "filter_test.html", { "name": name, "age": None, "hobby_list": hobby, "filesize": file_size, "test_html": test_html, })
filter_test.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Filter 测试</title> </head> <body> <h1>Filter 测试页面</h1> <!-- default --> {{ name }} <br> {{ name1|default:"这里不存在" }} <br> {{ age|default:"这里也不存在" }} <br> <!-- length --> {{ name|length }} <br> {{ hobby_list|length }} <br> {{ age|length }} <br> <!-- 切片 --> {{ hobby_list|slice:"0:2" }} <br> {{ name|slice:"0:2" }} <br> <!-- filesizeformat --> {{ filesize|filesizeformat }} <br> <!-- cut --> {{ name|cut:"ac" }} <br> {{ hobby_list|cut:"Movie" }} <br> <!-- join --> {{ name|join:"+" }} <br> {{ hobby_list|join:"-" }} <br> <!-- safe --> {{ test_html }} <br> {{ test_html|safe }} </body> </html>
运行效果:
没加 safe 的话就被 Django 默认转义了,加了 safe,a 标签就没被转义
truncatechars:
通过参数指定截断字符数,将多于指定的字符数的字符串字符进行截断,被截断的内容用 “...” 来代替
views.py:
from django.shortcuts import render, redirect, HttpResponse from app01 import models # Filter 测试 def filter_test(request): name = "Jack" age = 18 file_size = 988222836293733 test_html = "<a href='www.baidu.com'>这是一个 html 标签</a>" hobby = ["Reading", "Basketball", "Movie", "Music"] text = "abcdefghijklmnopqrstuvwxyz" return render( request, "filter_test.html", { "name": name, "age": None, "hobby_list": hobby, "filesize": file_size, "test_html": test_html, "text": text, })
filter_test.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Filter 测试</title> </head> <body> <h1>Filter 测试页面</h1> <!-- default --> {{ name }} <br> {{ name1|default:"这里不存在" }} <br> {{ age|default:"这里也不存在" }} <br> <!-- length --> {{ name|length }} <br> {{ hobby_list|length }} <br> {{ age|length }} <br> <!-- 切片 --> {{ hobby_list|slice:"0:2" }} <br> {{ name|slice:"0:2" }} <br> <!-- filesizeformat --> {{ filesize|filesizeformat }} <br> <!-- cut --> {{ name|cut:"ac" }} <br> {{ hobby_list|cut:"Movie" }} <br> <!-- join --> {{ name|join:"+" }} <br> {{ hobby_list|join:"-" }} <br> <!-- safe --> {{ test_html }} <br> {{ test_html|safe }} <br> <!-- truncatechars --> {{ text|truncatechars:10 }} </body> </html>
运行结果:
date:
对时间进行格式化
views.py:
from django.shortcuts import render, redirect, HttpResponse from app01 import models import datetime # Filter 测试 def filter_test(request): name = "Jack" age = 18 file_size = 988222836293733 test_html = "<a href='www.baidu.com'>这是一个 html 标签</a>" hobby = ["Reading", "Basketball", "Movie", "Music"] text = "abcdefghijklmnopqrstuvwxyz" now_time = datetime.datetime.now() return render( request, "filter_test.html", { "name": name, "age": None, "hobby_list": hobby, "filesize": file_size, "test_html": test_html, "text": text, "now_time": now_time, })
filter_test.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Filter 测试</title> </head> <body> <h1>Filter 测试页面</h1> <!-- default --> {{ name }} <br> {{ name1|default:"这里不存在" }} <br> {{ age|default:"这里也不存在" }} <br> <!-- length --> {{ name|length }} <br> {{ hobby_list|length }} <br> {{ age|length }} <br> <!-- 切片 --> {{ hobby_list|slice:"0:2" }} <br> {{ name|slice:"0:2" }} <br> <!-- filesizeformat --> {{ filesize|filesizeformat }} <br> <!-- cut --> {{ name|cut:"ac" }} <br> {{ hobby_list|cut:"Movie" }} <br> <!-- join --> {{ name|join:"+" }} <br> {{ hobby_list|join:"-" }} <br> <!-- safe --> {{ test_html }} <br> {{ test_html|safe }} <br> <!-- truncatechars --> {{ text|truncatechars:10 }} <br> <!-- 时间 --> {{ now_time|date:"Y-m-d H:i:s"}} </body> </html>
运行结果:
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python – Django – 模板语言之 Filters(过滤器) - Python技术站