best_filters.truncat¶
-
best_filters.
truncat
(str, pattern)¶ truncate the string at the specified pattern
Useful with filters timesince and timeuntil pattern is a regex expression string Do not forget to escape the dot (.) if it the char you want to search
Examples
>>> c = {'str':'abc...xyz'} >>> t = '{% load best_filters %}{{ str|truncat:"\." }}' >>> Template(t).render(Context(c)) 'abc'
>>> c = {'t1':datetime(1789,7,14),'t2':datetime(2018,1,21)} >>> t = '''{% load best_filters %} ... timesince with 2 terms : {{ t1|timesince:t2 }} ... timesince with 1 term : {{ t1|timesince:t2|truncat:"," }}''' >>> print(Template(t).render(Context(c))) timesince with 2 terms : 228 years, 6 months timesince with 1 term : 228 years