Django - Timeuntil Tag output abbreviation

心不动则不痛 提交于 2019-12-02 06:26:10

问题


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 ?


回答1:


Take a look to 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'))
)

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

In your template:

{{ somedata | timeuntil | my_time_abbr }}

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



来源:https://stackoverflow.com/questions/23332302/django-timeuntil-tag-output-abbreviation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!