搜索
首页后端开发Python教程Django模板系统 — 过滤器

过滤器,变量的显示形式的改变

一、形式:小写

{{ name | lower }}


二、串联:先转义文本到HTML,再转换每行到

标签

{{ my_text|escape|linebreaks }}


三、过滤器的参数

显示前30个字

{{ bio | truncatewords:"30" }}

格式化

{{ pub_date | date:"F j, Y" }}


过滤器列表

{{ 123|add:"5" }} 给value加上一个数值

{{ "AB'CD"|addslashes }} 单引号加上转义号,一般用于输出到javascript中

{{ "abcd"|capfirst }} 第一个字母大写

{{ "abcd"|center:"50" }} 输出指定长度的字符串,并把值对中

{{ "123spam456spam789"|cut:"spam" }} 查找删除指定字符串

{{ value|date:"F j, Y" }} 格式化日期

{{ value|default:"(N/A)" }} 值不存在,使用指定值

{{ value|default_if_none:"(N/A)" }} 值是None,使用指定值

{{ 列表变量|dictsort:"数字" }} 排序从小到大

{{ 列表变量|dictsortreversed:"数字" }} 排序从大到小

{% if 92|pisibleby:"2" %} 判断是否整除指定数字

{{ string|escape }} 转换为html实体

{{ 21984124|filesizeformat }} 以1024为基数,计算最大值,保留1位小数,增加可读性

{{ list|first }} 返回列表第一个元素

{{ "ik23hr&jqwh"|fix_ampersands }} &转为&

{{ 13.414121241|floatformat }} 保留1位小数,可为负数,几种形式

{{ 13.414121241|floatformat:"2" }} 保留2位小数

{{ 23456 |get_digit:"1" }} 从个位数开始截取指定位置的1个数字

{{ list|join:", " }} 用指定分隔符连接列表

{{ list|length }} 返回列表个数

{% if 列表|length_is:"3" %} 列表个数是否指定数值

{{ "ABCD"|linebreaks }} 用新行用


标记包裹

{{ "ABCD"|linebreaksbr }} 用新行用
标记包裹

{{ 变量|linenumbers }} 为变量中每一行加上行号

{{ "abcd"|ljust:"50" }} 把字符串在指定宽度中对左,其它用空格填充

{{ "ABCD"|lower }} 小写

{% for i in "1abc1"|make_list %}ABCDE,{% endfor %} 把字符串或数字的字符个数作为一个列表

{{ "abcdefghijklmnopqrstuvwxyz"|phone2numeric }} 把字符转为可以对应的数字??

{{ 列表或数字|pluralize }} 单词的复数形式,如列表字符串个数大于1,返回s,否则返回空串

{{ 列表或数字|pluralize:"es" }} 指定es

{{ 列表或数字|pluralize:"y,ies" }} 指定ies替换为y

{{ object|pprint }} 显示一个对象的值

{{ 列表|random }} 返回列表的随机一项

{{ string|removetags:"br p p" }} 删除字符串中指定html标记

{{ string|rjust:"50" }} 把字符串在指定宽度中对右,其它用空格填充

{{ 列表|slice:":2" }} 切片

{{ string|slugify }} 字符串中留下减号和下划线,其它符号删除,空格用减号替换

{{ 3|stringformat:"02i" }} 字符串格式,使用Python的字符串格式语法

{{ "EABD"|striptags }} 剥去[X]HTML语法标记

{{ 时间变量|time:"P" }} 日期的时间部分格式

{{ datetime|timesince }} 给定日期到现在过去了多少时间

{{ datetime|timesince:"other_datetime" }} 两日期间过去了多少时间

{{ datetime|timeuntil }} 给定日期到现在过去了多少时间,与上面的区别在于2日期的前后位置。

{{ datetime|timeuntil:"other_datetime" }} 两日期间过去了多少时间

{{ "abdsadf"|title }} 首字母大写

{{ "A B C D E F"|truncatewords:"3" }} 截取指定个数的单词

{{ "111221"|truncatewords_html:"2" }} 截取指定个数的html标记,并补完整

  • {{ list|unordered_list }}

多重嵌套列表展现为html的无序列表

{{ string|upper }} 全部大写

linkage url编码

{{ string|urlize }} 将URLs由纯文本变为可点击的链接。(没有实验成功)

{{ string|urlizetrunc:"30" }} 同上,多个截取字符数。(同样没有实验成功)

{{ "B C D E F"|wordcount }} 单词数

{{ "a b c d e f g h i j k"|wordwrap:"5" }} 每指定数量的字符就插入回车符

{{ boolean|yesno:"Yes,No,Perhaps" }} 对三种值的返回字符串,对应是 非空,空,None


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
可以在Python数组中存储哪些数据类型?可以在Python数组中存储哪些数据类型?Apr 27, 2025 am 12:11 AM

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

如果您尝试将错误的数据类型的值存储在Python数组中,该怎么办?如果您尝试将错误的数据类型的值存储在Python数组中,该怎么办?Apr 27, 2025 am 12:10 AM

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

Python标准库的哪一部分是:列表或数组?Python标准库的哪一部分是:列表或数组?Apr 27, 2025 am 12:03 AM

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

您应该检查脚本是否使用错误的Python版本执行?您应该检查脚本是否使用错误的Python版本执行?Apr 27, 2025 am 12:01 AM

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

在Python阵列上可以执行哪些常见操作?在Python阵列上可以执行哪些常见操作?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

在哪些类型的应用程序中,Numpy数组常用?在哪些类型的应用程序中,Numpy数组常用?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

您什么时候选择在Python中的列表上使用数组?您什么时候选择在Python中的列表上使用数组?Apr 26, 2025 am 12:12 AM

useanArray.ArarayoveralistinpythonwhendeAlingwithHomeSdata,performance-Caliticalcode,orinterFacingWithCcccode.1)同质性data:arrayssavememorywithtypedelements.2)绩效code-performance-clitionalcode-clitadialcode-critical-clitical-clitical-clitical-clitaine code:araysofferferbetterperperperformenterperformanceformanceformancefornalumericalicalialical.3)

所有列表操作是否由数组支持,反之亦然?为什么或为什么不呢?所有列表操作是否由数组支持,反之亦然?为什么或为什么不呢?Apr 26, 2025 am 12:05 AM

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactssperformance.2)listssdonotguaranteeconeeconeconstanttanttanttanttanttanttanttanttimecomplecomecomecomplecomecomecomecomecomecomplecomectaccesslikearrikearraysodo。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中