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

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

问题描述

我使用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的timesince源代码

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

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 Tag输出缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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