{"title":"django.contrib.humanize","version":"6.1","locale":"zh-hans","docname":"ref/contrib/humanize","url":"/zh-hans/6.1/ref/contrib/humanize/","canonical":"https://djangodocs.dev/zh-hans/6.1/ref/contrib/humanize/","summary":"一套 Django 模板过滤器，用于为数据添加 “人情味”。要激活这些过滤器，请在你的 INSTALLED_APPS 配置中添加 'django.contrib.humanize'。","html":"<span id=\"django-contrib-humanize\"></span><h1><code class=\"docutils literal notranslate\">django.contrib.humanize</code><a class=\"heading-anchor\" href=\"#module-django.contrib.humanize\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>一套 Django 模板过滤器，用于为数据添加 “人情味”。</p>\n<p>要激活这些过滤器，请在你的 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> 配置中添加 <code class=\"docutils literal notranslate\">'django.contrib.humanize'</code>。添加完毕后，在模板中使用 <code class=\"docutils literal notranslate\">{% load humanize %}</code>，你就可以使用以下过滤器了。</p>\n<section id=\"apnumber\">\n<span id=\"std-templatefilter-apnumber\"></span><h2><code class=\"docutils literal notranslate\">apnumber</code><a class=\"heading-anchor\" href=\"#apnumber\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>对于数字 1-9，返回拼出的数字。否则，返回数字。这遵循了美联社的风格。</p>\n<p>举例：</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">1</code> 变成 <code class=\"docutils literal notranslate\">one</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">2</code> 变成 <code class=\"docutils literal notranslate\">two</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">10</code> 变成 <code class=\"docutils literal notranslate\">10</code>。</p></li>\n</ul>\n<p>你可以传入一个整数或整数的字符串表示。</p>\n</section>\n<section id=\"intcomma\">\n<span id=\"std-templatefilter-intcomma\"></span><h2><code class=\"docutils literal notranslate\">intcomma</code><a class=\"heading-anchor\" href=\"#intcomma\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>将一个整数或浮点数（或其中之一的字符串表示法）转换为每三位数包含逗号的字符串。</p>\n<p>举例：</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">4500</code> 变成 <code class=\"docutils literal notranslate\">4,500</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">4500.2</code> 变成 <code class=\"docutils literal notranslate\">4,500.2</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">45000</code> 变成 <code class=\"docutils literal notranslate\">45,000</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">450000</code> 变成 <code class=\"docutils literal notranslate\">450,000</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">4500000</code> 变成 <code class=\"docutils literal notranslate\">4,500,000</code>。</p></li>\n</ul>\n<p><a class=\"reference internal\" href=\"/zh-hans/6.1/topics/i18n/formatting/\"><span class=\"doc\">本地格式化</span></a> 如果启用，例如使用 <code class=\"docutils literal notranslate\">''de</code> 语言，将得到尊重：</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">45000</code> 变成 <code class=\"docutils literal notranslate\">'45.000'</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">450000</code> 变成 <code class=\"docutils literal notranslate\">'450.000'</code>。</p></li>\n</ul>\n</section>\n<section id=\"intword\">\n<span id=\"std-templatefilter-intword\"></span><h2><code class=\"docutils literal notranslate\">intword</code><a class=\"heading-anchor\" href=\"#intword\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>将大整数（或整数的字符串表示）转换为友好的文本表示。将 <code class=\"docutils literal notranslate\">1.0</code> 翻译成单数，将所有其他数值翻译成复数，这对某些语言可能不正确。对于 100 万以上的数字效果最好。</p>\n<p>举例：</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">1000000</code> 变成 <code class=\"docutils literal notranslate\">1.0 million</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">1200000</code> 变成 <code class=\"docutils literal notranslate\">1.2 million</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">1200000000</code> 变成 <code class=\"docutils literal notranslate\">1.2 billion</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">-1200000000</code> 变成 <code class=\"docutils literal notranslate\">-1.2 billion</code>。</p></li>\n</ul>\n<p>支持最高 10^100（Googol）的数值。</p>\n<p><a class=\"reference internal\" href=\"/zh-hans/6.1/topics/i18n/formatting/\"><span class=\"doc\">本地格式化</span></a> 如果启用，例如使用 <code class=\"docutils literal notranslate\">''de</code> 语言，将得到尊重：</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">1000000</code> 变成 <code class=\"docutils literal notranslate\">'1,0 Million'</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">1200000</code> 变成 <code class=\"docutils literal notranslate\">'1,2 Millionen'</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">1200000000</code> 变成 <code class=\"docutils literal notranslate\">'1,2 Milliarden'</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">-1200000000</code> 变成 <code class=\"docutils literal notranslate\">'-1,2 Milliarden'</code>。</p></li>\n</ul>\n</section>\n<section id=\"naturalday\">\n<span id=\"std-templatefilter-naturalday\"></span><h2><code class=\"docutils literal notranslate\">naturalday</code><a class=\"heading-anchor\" href=\"#naturalday\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>对于当天或一天之内的日期，根据情况返回 “today”、“tomorrow” 或 “yesterday”。否则，使用传入的格式字符串格式化日期。</p>\n<p><strong>参数：</strong> 日期格式化字符串，如 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/templates/builtins/#std-templatefilter-date\"><code class=\"xref std std-tfilter docutils literal notranslate\">date</code></a> 标签所述。</p>\n<p>例子（当 'today' 是 2007 年 2 月 17 日）：</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">16 Feb 2007</code> 变成 <code class=\"docutils literal notranslate\">yesterday</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">17 Feb 2007</code> 变成 <code class=\"docutils literal notranslate\">today</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">18 Feb 2007</code> 变成 <code class=\"docutils literal notranslate\">tomorrow</code>。</p></li>\n<li><p>任何其他的日子都会根据给定的参数进行格式化，如果没有给定参数，则根据 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-DATE_FORMAT\"><code class=\"xref std std-setting docutils literal notranslate\">DATE_FORMAT</code></a> 配置。</p></li>\n</ul>\n</section>\n<section id=\"naturaltime\">\n<span id=\"std-templatefilter-naturaltime\"></span><h2><code class=\"docutils literal notranslate\">naturaltime</code><a class=\"heading-anchor\" href=\"#naturaltime\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>对于日期时间值，返回一个字符串，代表多少秒、多少分钟或多少小时前 —— 如果值超过一天，则返回到 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/templates/builtins/#std-templatefilter-timesince\"><code class=\"xref std std-tfilter docutils literal notranslate\">timesince</code></a> 格式。如果日期时间值是在未来，返回值将自动使用一个适当的短语。</p>\n<p>例子（当 'now' 是 2007 年 2 月 17 日 16:30:00）：</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">17 Feb 2007 16:30:00</code> 变成 <code class=\"docutils literal notranslate\">now</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">17 Feb 2007 16:29:31</code> 变成 <code class=\"docutils literal notranslate\">29 seconds ago</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">17 Feb 2007 16:29:00</code> 变成 <code class=\"docutils literal notranslate\">a minute ago</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">17 Feb 2007 16:25:35</code> 变成 <code class=\"docutils literal notranslate\">4 minutes ago</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">17 Feb 2007 15:30:29</code> 变成 <code class=\"docutils literal notranslate\">59 minutes ago</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">17 Feb 2007 15:30:01</code> 变成 <code class=\"docutils literal notranslate\">59 minutes ago</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">17 Feb 2007 15:30:00</code> 变成 <code class=\"docutils literal notranslate\">an hour ago</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">17 Feb 2007 13:31:29</code> 变成 <code class=\"docutils literal notranslate\">2 hours ago</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">16 Feb 2007 13:31:29</code> 变成 <code class=\"docutils literal notranslate\">1 day, 2 hours ago</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">16 Feb 2007 13:30:01</code> 变成 <code class=\"docutils literal notranslate\">1 day, 2 hours ago</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">16 Feb 2007 13:30:00</code> 变成 <code class=\"docutils literal notranslate\">1 day, 3 hours ago</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">17 Feb 2007 16:30:30</code> 变成 <code class=\"docutils literal notranslate\">30 seconds from now</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">17 Feb 2007 16:30:29</code> 变成 <code class=\"docutils literal notranslate\">29 seconds from now</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">17 Feb 2007 16:31:00</code> 变成 <code class=\"docutils literal notranslate\">a minute from now</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">17 Feb 2007 16:34:35</code> 变成 <code class=\"docutils literal notranslate\">4 minutes from now</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">17 Feb 2007 17:30:29</code> 变成 <code class=\"docutils literal notranslate\">an hour from now</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">17 Feb 2007 18:31:29</code> 变成 <code class=\"docutils literal notranslate\">2 hours from now</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">18 Feb 2007 16:31:29</code> 变成 <code class=\"docutils literal notranslate\">1 day from now</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">26 Feb 2007 18:31:29</code> 变成 <code class=\"docutils literal notranslate\">1 week, 2 days from now</code>。</p></li>\n</ul>\n</section>\n<section id=\"ordinal\">\n<span id=\"std-templatefilter-ordinal\"></span><h2><code class=\"docutils literal notranslate\">ordinal</code><a class=\"heading-anchor\" href=\"#ordinal\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>将一个整数转换为字符串的序数。</p>\n<p>举例：</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">1</code> 变成 <code class=\"docutils literal notranslate\">1st</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">2</code> 变成 <code class=\"docutils literal notranslate\">2nd</code>。</p></li>\n<li><p><code class=\"docutils literal notranslate\">3</code> 变成 <code class=\"docutils literal notranslate\">3rd</code>。</p></li>\n</ul>\n<p>你可以传入整数或整数的字符串表示。负整数将原样返回。</p>\n</section>","rootId":"module-django.contrib.humanize","toc":[{"title":"apnumber","anchor":"apnumber","children":[]},{"title":"intcomma","anchor":"intcomma","children":[]},{"title":"intword","anchor":"intword","children":[]},{"title":"naturalday","anchor":"naturalday","children":[]},{"title":"naturaltime","anchor":"naturaltime","children":[]},{"title":"ordinal","anchor":"ordinal","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API 参考","url":"/zh-hans/6.1/ref/"},{"docname":"ref/contrib/index","title":"contrib 包","url":"/zh-hans/6.1/ref/contrib/"}],"prev":{"docname":"ref/contrib/gis/deployment","title":"部署 GeoDjango","url":"/zh-hans/6.1/ref/contrib/gis/deployment/"},"next":{"docname":"ref/contrib/messages","title":"消息框架","url":"/zh-hans/6.1/ref/contrib/messages/"},"formats":{"html":"/zh-hans/6.1/ref/contrib/humanize/","markdown":"/zh-hans/6.1/ref/contrib/humanize.md","json":"/zh-hans/6.1/ref/contrib/humanize.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/ref/contrib/humanize.txt","official":"https://docs.djangoproject.com/zh-hans/6.1/ref/contrib/humanize/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}