Display and format Django DurationField in a template

Im using Django 1.8, and I have one of my field defined as DurationField, but I have not found a way to display it correctly on my template if I output it like this:

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

is there any filter or some other way to display something more like

 2hours 30 min 
+5
source share
1 answer

No, I do not think that there is a built-in filter for formatting timedelta , but it is quite easy to write it yourself.

Here is a basic (and unverified) example:

 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) 
+9
source

Source: https://habr.com/ru/post/1233623/


All Articles