Django - Timeuntil 标签输出缩写 [英] Django - Timeuntil Tag output abbreviation

查看:12
本文介绍了Django - Timeuntil 标签输出缩写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 django timeuntil 模板标签,输出类似于 8 小时 15 分钟.有谁知道我如何使输出像 8 Hr, 15 Min 一样?

I'm using django timeuntil template tag and the output is something like 8 hours, 15 minutes. Does anyone have any idea how I can make the output to be like 8 Hr, 15 Min ?

推荐答案

看看 django 源代码中的时间:

chunks = (
    (60 * 60 * 24 * 365, ungettext_lazy('%d year', '%d years')),
    (60 * 60 * 24 * 30, ungettext_lazy('%d month', '%d months')),
    (60 * 60 * 24 * 7, ungettext_lazy('%d week', '%d weeks')),
    (60 * 60 * 24, ungettext_lazy('%d day', '%d days')),
    (60 * 60, ungettext_lazy('%d hour', '%d hours')),
    (60, ungettext_lazy('%d minute', '%d minutes'))
)

更改它的快速简便的方法是 编写了您的自定义模板过滤器以通过 Hr 更改 hours:

The fast and easy way to change it is wrote your custom template filter to change hours by Hr:

def my_time_abbr(value): 
    return value.replace( 'hours', 'Hr').replace('minutes','Min')

在您的模板中:

{{ somedata | timeuntil | my_time_abbr }}

如果您在国际化模式下工作,您还可以从头开始重写 timesince 过滤器(从 django timesince 复制粘贴).

You can also rewrite timesince filter from scratch (copy paste from django timesince) if you are working in Internationalization mode.

这篇关于Django - Timeuntil 标签输出缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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