Django - Timeuntil Tag输出缩写

时间:2014-04-28 04:04:59

标签: django python-2.7 django-templates

我正在使用django timeuntil模板标记,输出类似于8 hours, 15 minutes。有谁知道如何使输出像8 Hr, 15 Min

1 个答案:

答案 0 :(得分:3)

看看timesince from django source code

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'))
)

快速简便的更改方式是wrote your custom template filter hours改变Hr

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

在你的模板中:

{{ somedata | timeuntil | my_time_abbr }}

如果您正在进行国际化模式,也可以从头开始重写timesince过滤器(从django timesince复制粘贴)。