gpt4 book ai didi

python - 在模板中显示和格式化 Django DurationField

转载 作者:太空狗 更新时间:2023-10-29 20:53:45 25 4
gpt4 key购买 nike

我使用的是 Django 1.8,我将我的字段之一定义为 DurationField,但我找不到任何方法在我的模板上正确显示它,如果我这样输出它:

{{runtime}}
i just get 0:00:00.007980

是否有任何过滤器或任何其他方式显示更像的东西

2hours 30 min

最佳答案

不,我不认为有任何内置的过滤器来格式化 timedelta,不过你自己写一个应该很容易。

这是一个基本的例子:

from django import template


register = template.Library()


@register.filter
def duration(td):
total_seconds = int(td.total_seconds())
hours = total_seconds // 3600
minutes = (total_seconds % 3600) // 60

return '{} hours {} min'.format(hours, minutes)


关于python - 在模板中显示和格式化 Django DurationField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33105457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com