时代滤镜的格式 [英] Format of timesince filter

查看:100
本文介绍了时代滤镜的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用 {{date / timesince}} 过滤器,而不是有两个相邻的单位,只显示一个?

Is there a way to use the {{date|timesince}} filter, but instead of having two adjacent units, only display one?

例如,我的模板正在显示18小时16分钟。如何显示18小时? (舍入不在这里。)谢谢。

For example, my template is currently displaying "18 hours, 16 minutes". How would I get it to display "18 hours"? (Rounding is not a concern here.) Thank you.

推荐答案

我不能想到一个简单的内置方式来做。这是一个自定义过滤器,我有时会发现有用:

I can't think of a simple builtin way to do this. Here's a custom filter I've sometimes found useful:

from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter
@stringfilter
def upto(value, delimiter=None):
    return value.split(delimiter)[0]
upto.is_safe = True

然后你可以做

{{ date|timesince|upto:',' }}

这篇关于时代滤镜的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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