Login

Filter to shorten Natural Time

Author:
abhiramr
Posted:
June 26, 2018
Language:
Python
Version:
1.10
Score:
0 (after 0 ratings)
  • This is a filter to shorten the natural time value obtained using naturaltime function from humanize.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#In templatetag file , say file_tags.py
from django import template
register = template.Library()

@register.filter
def shorten_naturaltime(naturaltime):
    naturaltime = naturaltime.replace('minutes','m').replace('hours','h').replace('days','d')
    naturaltime = naturaltime.replace('months','mon').replace('weeks','w').replace('week','w')
    return naturaltime

#In Usage file
{% load file_tags %} 
{{start_time|naturaltime|shorten_naturaltime}} #where start_time is the datetime object to be modified

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.