django.contrib.humanize¶
一套 Django 模板过滤器,用于为数据添加 “人情味”。
要激活这些过滤器,请在你的 INSTALLED_APPS 配置中添加 'django.contrib.humanize'。添加完毕后,在模板中使用 {% load humanize %},你就可以使用以下过滤器了。
apnumber¶
对于数字 1-9,返回拼出的数字。否则,返回数字。这遵循了美联社的风格。
举例:
1变成one。2变成two。10变成10。
你可以传入一个整数或整数的字符串表示。
intcomma¶
将一个整数或浮点数(或其中之一的字符串表示法)转换为每三位数包含逗号的字符串。
举例:
4500变成4,500。4500.2变成4,500.2。45000变成45,000。450000变成450,000。4500000变成4,500,000。
本地格式化 如果启用,例如使用 ''de 语言,将得到尊重:
45000变成'45.000'。450000变成'450.000'。
intword¶
将大整数(或整数的字符串表示)转换为友好的文本表示。将 1.0 翻译成单数,将所有其他数值翻译成复数,这对某些语言可能不正确。对于 100 万以上的数字效果最好。
举例:
1000000变成1.0 million。1200000变成1.2 million。1200000000变成1.2 billion。-1200000000变成-1.2 billion。
支持最高 10^100(Googol)的数值。
本地格式化 如果启用,例如使用 ''de 语言,将得到尊重:
1000000变成'1,0 Million'。1200000变成'1,2 Millionen'。1200000000变成'1,2 Milliarden'。-1200000000变成'-1,2 Milliarden'。
naturalday¶
对于当天或一天之内的日期,根据情况返回 “today”、“tomorrow” 或 “yesterday”。否则,使用传入的格式字符串格式化日期。
例子(当 'today' 是 2007 年 2 月 17 日):
16 Feb 2007变成yesterday。17 Feb 2007变成today。18 Feb 2007变成tomorrow。- 任何其他的日子都会根据给定的参数进行格式化,如果没有给定参数,则根据
DATE_FORMAT配置。
naturaltime¶
对于日期时间值,返回一个字符串,代表多少秒、多少分钟或多少小时前 —— 如果值超过一天,则返回到 timesince 格式。如果日期时间值是在未来,返回值将自动使用一个适当的短语。
例子(当 'now' 是 2007 年 2 月 17 日 16:30:00):
17 Feb 2007 16:30:00变成now。17 Feb 2007 16:29:31变成29 seconds ago。17 Feb 2007 16:29:00变成a minute ago。17 Feb 2007 16:25:35变成4 minutes ago。17 Feb 2007 15:30:29变成59 minutes ago。17 Feb 2007 15:30:01变成59 minutes ago。17 Feb 2007 15:30:00变成an hour ago。17 Feb 2007 13:31:29变成2 hours ago。16 Feb 2007 13:31:29变成1 day, 2 hours ago。16 Feb 2007 13:30:01变成1 day, 2 hours ago。16 Feb 2007 13:30:00变成1 day, 3 hours ago。17 Feb 2007 16:30:30变成30 seconds from now。17 Feb 2007 16:30:29变成29 seconds from now。17 Feb 2007 16:31:00变成a minute from now。17 Feb 2007 16:34:35变成4 minutes from now。17 Feb 2007 17:30:29变成an hour from now。17 Feb 2007 18:31:29变成2 hours from now。18 Feb 2007 16:31:29变成1 day from now。26 Feb 2007 18:31:29变成1 week, 2 days from now。