English 中文(简体)
在模板中添加两个时间价值和显示
原标题:Add two time values and display in template

我对如何在模板中正确显示两个时间价值感到不安。 这里是我的法典。

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

我尝试使用:

{{ get_monday_total }}
{{ UserTimesheet.get_monday_total }}

还有许多其他事情,但对于我来说,我无法说明如何在一个模板中展示这一逻辑。 任何想法?

感谢一切帮助。


http://www.ohchr.org。

意见。 y

@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})

例如,我希望产出的是17:00 - 14:30和产出2:30,如时间表所示,有人在14:30离开,17:00。 我想表明总的工作时间,就这样说是2.5小时。

问题回答

第一个问题是,你不能将2个<代码> 时间下。 工作方式是使用<代码> 具体时间:

from datetime import datetime, time, date

monday_start_time = time(1, 1, 1, 1)
monday_end_time = time(2, 2, 2, 2)
delta = (datetime.combine(date.min, monday_start_time) - datetime.combine(date.min, monday_end_time))
# datetime.timedelta(days=-1, seconds=82738, microseconds=999999)

I m not sure exactly what information you want to display, but you can now use this delta as is or for further operation.

此外,自您通过<代码>queryset以来 (自以来,应重新命名为>。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签