I am having trouble on how to correctly display the addition of two time values in a template. Here is my code.
models.py
class UserTimesheet(models.Model):
employee = models.ForeignKey(Employee, models.SET_NULL, blank=True, null=True)
date = models.DateField()
monday_start_time = models.TimeField(_(u"Start Time"), null=True, blank=True)
monday_end_time = models.TimeField(_(u"End Time"), null=True, blank=True, )
@property
def get_monday_total(self):
if self.monday_start_time is not None and self.monday_end_time is not None:
return self.monday_end_time - self.monday_start_time
I have tried using:
{{ get_monday_total }}
{{ UserTimesheet.get_monday_total }}
And a lot of different other things, but I cannot, for the life of me, figure out how to display this logic within a template. Any ideas?
Any and all help is grateful.
EDIT
views.py
@login_required(login_url="/login")
def manage_timesheet(request, pk):
time = UserTimesheet.objects.all()
queryset = UserTimesheet.objects.get(id=pk)
form = Timesheet(instance=queryset)
if request.method == 'POST':
form = Timesheet(request.POST, instance=queryset)
if form.is_valid():
form.save()
return redirect('/timehub')
return render(request, 'newtimesheet/manage_timesheet.html', {"form": form, "queryset": queryset, "time": time})
What I want to output for example is 17:00 - 14:30 and output 2:30, like in a timesheet, someone comes in at 14:30 and leave at 17:00. I want to show the total amount of times worked, which in this example is 2.5 hours.
The first problem is that you cannot subtract two
time. A workaround is to usedatetime:Furthermore, since your passing
queryset(which should be renamed since.getreturns an instance, not a queryset), you should use{{ queryset.get_monday_total }}in your template.