问题
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