Format of timesince filter

后端 未结 3 1377
你的背包
你的背包 2021-02-02 01:04

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

For example, my template is currently displ

3条回答
  •  走了就别回头了
    2021-02-02 01:52

    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
    

    Then you could just do

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

提交回复
热议问题