django.contrib.humanize

一组Django模板过滤器,可用于为数据添加“人性化接触”。

要激活这些过滤器,请将'django.contrib.humanize'添加到INSTALLED_APPS设置中。 一旦你完成了,使用{% 加载 人性化 %}一个模板,你可以访问下面的过滤器。

apnumber

对于数字1-9,返回拼出的数字。 否则,返回号码。 这跟随美联社风格。

例子:

  • 1 becomes one.
  • 2 becomes two.
  • 10 becomes 10.

您可以传入整型或整型的字符串表示形式。

intcomma

将整数或浮点数(或任一个的字符串表示形式)转换为每三位数字包含逗号的字符串。

例子:

  • 4500 becomes 4,500.
  • 4500.2 becomes 4,500.2.
  • 45000 becomes 45,000.
  • 450000 becomes 450,000.
  • 4500000 becomes 4,500,000.

Format localization will be respected if enabled, e.g. with the 'de' language:

  • 45000 becomes '45.000'.
  • 450000 becomes '450.000'.

intword

将大整数(或整数的字符串表示形式)转换为友好的文本表示形式。 适用于超过100万的数字。

例子:

  • 1000000 becomes 1.0 million.
  • 1200000 becomes 1.2 million.
  • 1200000000 becomes 1.2 billion.

支持高达10 ^ 100(Googol)的值。

Format localization will be respected if enabled, e.g. with the 'de' language:

  • 1000000 becomes '1,0 Million'.
  • 1200000 becomes '1,2 Million'.
  • 1200000000 becomes '1,2 Milliarden'.

naturalday

对于当天或一天内的日期,请酌情返回“今天”,“明天”或“昨天”。 否则,使用传入的格式字符串格式化日期。

论据: 日期格式化字符串,如date标签中所述。

例子(“今天”是2007年2月17日):

  • 16 Feb 2007 becomes yesterday.
  • 17 Feb 2007 becomes today.
  • 18 Feb 2007 becomes tomorrow.
  • 如果没有给出任何参数,则任何其他日期都将根据给定的参数或DATE_FORMAT设置进行格式化。

naturaltime

对于日期时间值,返回表示多少秒,几分钟或几小时前的字符串 - 如果值超过一天,则返回到timesince格式。 在日期时间值将来的情况下,返回值将自动使用适当的短语。

示例(当“现在”是2007年2月17日16:30:00):

  • 17 Feb 2007 16:30:00 becomes now.
  • 17 Feb 2007 16:29:31 becomes 29 seconds ago.
  • 17 Feb 2007 16:29:00 becomes a minute ago.
  • 17 Feb 2007 16:25:35 becomes 4 minutes ago.
  • 17 Feb 2007 15:30:29 becomes 59 minutes ago.
  • 17 Feb 2007 15:30:01 becomes 59 minutes ago.
  • 17 Feb 2007 15:30:00 becomes an hour ago.
  • 17 Feb 2007 13:31:29 becomes 2 hours ago.
  • 16 Feb 2007 13:31:29 becomes 1 day, 2 hours ago.
  • 16 Feb 2007 13:30:01 becomes 1 day, 2 hours ago.
  • 16 Feb 2007 13:30:00 becomes 1 day, 3 hours ago.
  • 17 Feb 2007 16:30:30 becomes 30 seconds from now.
  • 17 Feb 2007 16:30:29 becomes 29 seconds from now.
  • 17 Feb 2007 16:31:00 becomes a minute from now.
  • 17 Feb 2007 16:34:35 becomes 4 minutes from now.
  • 17 Feb 2007 17:30:29 becomes an hour from now.
  • 17 Feb 2007 18:31:29 becomes 2 hours from now.
  • 18 Feb 2007 16:31:29 becomes 1 day from now.
  • 26 Feb 2007 18:31:29 becomes 1 week, 2 days from now.

ordinal

将一个整数转换为一个字符串的序数。

例子:

  • 1 becomes 1st.
  • 2 becomes 2nd.
  • 3 becomes 3rd.

您可以传入整型或整型的字符串表示形式。