I am writing a custom template tags in django. Now, I explain you briefly. My final goal is to have in my template a duration field as hours and minutes. At the moment, I have a duration field that provides also the seconds, which I am not interested to be shown. So the goal is 1 hour = 01:00 but at the moment I have 01:00:00. I wrote this function which is working when I use it in my views but, I need also to use it inside django template.
custom_tags.py
from django import template
register = template.Library()
@register.simple_tag
def duration(td):
total_seconds = int(td.total_seconds())
hours = total_seconds // 3600
minutes = (total_seconds % 3600) // 60
if minutes < 1:
minutes = '00'
elif minutes < 2:
minutes = '01'
elif minutes < 3:
minutes = '02'
elif minutes < 4:
minutes = '03'
elif minutes < 5:
minutes = '04'
elif minutes < 6:
minutes = '05'
elif minutes < 7:
minutes = '06'
elif minutes < 8:
minutes = '07'
elif minutes < 9:
minutes = '08'
elif minutes < 10:
minutes = '09'
return '{}:{}'.format(hours, minutes)
template
<td>
{% if mix.mission.duration_dual is not None %} <strong>{% duration 'mix.mission.duration_dual' %}</strong>{% endif %}
<div>{% if mix.log_entry.eet is not None and mix.log_entry.solo_flight == False %} {{mix.log_entry.eet|time:'G:i'}} {% endif %}</div>
</td>
The views and models are quite unuseful in this case, because everything is working fine, I need just to understand if there's a way to make the time field appearing right also in this case. The error I am having is:
'SafeString' object has no attribute 'total_seconds'
Traceback:
Traceback (most recent call last):
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/astergest/mission/views.py", line 71, in student_mission_ppl
return render(request, 'mission/student_mission_ppl.html', context)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 176, in render
return self._render(context)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 168, in _render
return self.nodelist.render(context)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 977, in render
return SafeString(''.join([
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 978, in <listcomp>
node.render_annotated(context) for node in self
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 938, in render_annotated
return self.render(context)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/loader_tags.py", line 153, in render
return compiled_parent._render(context)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 168, in _render
return self.nodelist.render(context)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 977, in render
return SafeString(''.join([
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 978, in <listcomp>
node.render_annotated(context) for node in self
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 938, in render_annotated
return self.render(context)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/loader_tags.py", line 65, in render
result = block.nodelist.render(context)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 977, in render
return SafeString(''.join([
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 978, in <listcomp>
node.render_annotated(context) for node in self
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 938, in render_annotated
return self.render(context)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/defaulttags.py", line 214, in render
nodelist.append(node.render_annotated(context))
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 938, in render_annotated
return self.render(context)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/defaulttags.py", line 295, in render
return nodelist.render(context)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 977, in render
return SafeString(''.join([
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 978, in <listcomp>
node.render_annotated(context) for node in self
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/base.py", line 938, in render_annotated
return self.render(context)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/venv/lib/python3.9/site-packages/django/template/library.py", line 193, in render
output = self.func(*resolved_args, **resolved_kwargs)
File "/Users/Giorgio 1/Documents/Programmazione/Astergest/astergest/mission/templatetags/custom_tags.py", line 7, in duration
total_seconds = int(td.total_seconds())
AttributeError: 'SafeString' object has no attribute 'total_seconds'
Thank you very much in advance for your help
CodePudding user response:
That because td is a string not a timedelta. You can fix by removing the quotes from tag as follows
<td>
{% if mix.mission.duration_dual is not None %}
<strong>{% duration mix.mission.duration_dual
%}</strong>{% endif %}
<div>{% if mix.log_entry.eet is not None and
mix.log_entry.solo_flight == False %}
{{mix.log_entry.eet|time:'G:i'}} {% endif %}.
</div>
</td>