Falsk 与 Django 过滤器的使用与区别详解
作者:Xcsg 时间:2023-08-21 08:06:02
1,flask中内置的过滤器模板中常用方法:
{#过滤器调用方式{{变量|过滤器名称}} #}
<!-- safe过滤器,可以禁用转义 -->
{{'<strong>hello</strong>'|safe}}
<br/>
大写转小写
{{'HELLO'|lower}}
小写转大写
{{'hello'|upper}}
<br/>
字符串反转
{{'abc'|reverse}}
首字母大写
{{'james'|capitalize}}
过滤HTML标签
{{'<em>apple</em>'| striptags}}
<br/>
只显示list首个元素
{{[100,95,55,81]|first}}
只显示list最后一个元素
{{[100,95,55,81]|last}}
显示list的长度
{{[100,95,55,81]|length}}
list里所有元素求和
{{[100,95,55,81]|sum}}
list进行排序
{{[100,95,55,81]|sort}}
format:格式化输出
{{ '%s is %d' | format('name',17) }}
<br/>
对语句块进行过滤
{% filter upper%}
qwedr,tyuio,piuytre,rtyuiuyt
afaife oifafa, afaf
{%endfilter%} <br/>
链式调用过滤器
{{'abc'|reverse|upper}}
<br><br>
自定义过滤器
{{100|my_filter}}
自定义过滤器(后端视图函数)
@app.template_filter("my_filter")
#自定义过滤器
def my_filter(val):
return '$' + str(val)
2,django中内置的过滤器模板中常用方法:
{#通过内置过滤器实现加减法#}
{{test_int | add:10}}
{{list.0}}
<br>
{%for i in list%}
{{i}}
{#打印索引 从1开始的计数器#}
{{forloop.counter}}
{#打印数组索性#}
{{forloop.counter0}}
{#倒序索引#}
{{forloop.revcounter}}
<br>
{%endfor%}
<br>
{% for i in dict%}
{#取首位和末尾 first last#}
姓名:{{i.name |first}} 年龄:{{i.age}}
{%endfor%}
<br>
{#大写小写#}
{{'jack' | upper | lower}}
{#返回长度#}
{{'123456'| length}}
{#截断操作#}
{{content | truncatechars:10}}
截断单词{{content | truncatewords:2}}
{# 格式化日期#}
{{dt | date:'Y-m-d H:i:s'}}
自定义过滤器(后端视图函数)
首先在工程文件的同名配置文件下创建templatetags文件夹,建立myfiler.py用来自定义过滤器
例:
from django import template
#注册过滤器对象
register = template.Library()
#自定义过滤器
#通过装饰器的方式来注册自定义过滤器
@register.filter
def my_str(val):
return "$" +str(val)
#定义一个双参数过滤器
@register.filter
def my_str_two(val,p1):
return val + p1
#定义一个多参数的过滤器
#注册过滤器的时候,需要注册SIMPLE_TAG
@register.simple_tag
def my_str_many(val,p1,p2):
return val + p1 + p2
#定义一个取余数的过滤器
@register.filter
def get_line(val):
return int(val) % 2
在templates文件夹中模板文件中导入
{#导入过滤器文件#}
{% load myfilter %}
{#导入过滤器文件#}
{% load my_filter %}
{{test_str | my_str}}
{# 两个参数进行调用#}
{{'你好' | my_str_two:"不好"}}
{#调用多参数的过滤器#}
{% my_str_many '你好' '不好' '啊' %}
{{test_list | length}}
来源:https://www.cnblogs.com/xcsg/p/10310897.html
标签:Falsk,Django,过滤器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL使用表锁和行锁的场景详解
2024-01-24 05:59:39
![](https://img.aspxhome.com/file/2023/3/122883_0s.gif)
python 爬取小说并下载的示例
2023-08-24 19:31:49
![](https://img.aspxhome.com/file/2023/2/61962_0s.gif)
Python中str.format()详解
2023-01-12 13:26:47
你不知道的Vue技巧之--开发一个可以通过方法调用的组件(推荐)
2024-04-30 08:46:07
Mysql学习之创建和操作数据库及表DDL大全小白篇
2024-01-25 01:16:15
Python爬虫之BeautifulSoup的基本使用教程
2022-03-27 18:35:31
![](https://img.aspxhome.com/file/2023/5/64645_0s.png)
AES加密算法的原理详解与实现分析
2022-11-18 14:39:43
![](https://img.aspxhome.com/file/2023/5/132495_0s.png)
Python实现基于POS算法的区块链
2023-10-30 01:47:19
![](https://img.aspxhome.com/file/2023/1/67291_0s.png)
获取SqlServer存储过程定义的三种方法
2024-01-24 06:03:38
go goroutine 怎样进行错误处理
2024-05-22 10:14:39
关于Python 位运算防坑指南
2022-06-13 08:06:55
基于Python实现自动化文档整理工具
2021-10-01 01:29:19
![](https://img.aspxhome.com/file/2023/7/120337_0s.png)
python3 配置logging日志类的操作
2021-04-22 20:15:09
![](https://img.aspxhome.com/file/2023/3/98853_0s.jpg)
Python详细讲解浅拷贝与深拷贝的使用
2023-01-01 03:00:10
![](https://img.aspxhome.com/file/2023/5/81635_0s.png)
终端能到import模块 解决jupyter notebook无法导入的问题
2022-11-19 19:26:29
![](https://img.aspxhome.com/file/2023/0/85060_0s.jpg)
玩转python selenium鼠标键盘操作(ActionChains)
2023-03-06 05:21:43
Opencv-Python图像透视变换cv2.warpPerspective的示例
2023-06-27 19:03:18
![](https://img.aspxhome.com/file/2023/2/98842_0s.gif)
用javascript获得地址参数的两种方法
2008-04-30 12:17:00
Python程序设计入门(4)模块和包
2023-06-18 11:37:36
numpy:找到指定元素的索引示例
2022-05-18 22:29:37