如何显示“这几个月前”在Django中使用Humanize? [英] How to display "This many months ago" in Django using Humanize?

查看:71
本文介绍了如何显示“这几个月前”在Django中使用Humanize?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个变量:

{{ video.pub_date }}

,其输出为:

May 16, 2011, 2:03 p.m.

如何显示:

1 month ago

我已在页面上加载了人性化,但是用于人性化的django文档并未真正显示如何实现它来显示我想要的内容:

I have loaded humanize on the page, but the django docs for humanize doesn't really show how to implement it to show what I want:

https://docs.djangoproject.com/en/dev/ref/contrib/humanize/

它只是说有可能在页面底部。

It just says that it's possible at the bottom of the page there.

推荐答案

您必须拥有的开发版本Django使用 自然时间过滤器

You have to have the development version of Django to use the naturaltime filter

{% load humanize %}
{{ video.pub_date|naturaltime }}

人性化不是模板标签,而是模板过滤器,这意味着在您加载模板时,您可以访问模块中包含的其他各种过滤器。

Humanize isn't a template tag, it's a library of template filters which means when you load it you have access to various other filters included in the module.

或者,您可以使用 时间过滤器,而不必加载任何其他模板标签库

Alternatively you can use the timesince filter withouth having to load any other template tag libraries

{{ video.pub_date|timesince }}

这篇关于如何显示“这几个月前”在Django中使用Humanize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆