Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何控制django中duration字段的格式_Python_Django - Fatal编程技术网

Python 如何控制django中duration字段的格式

Python 如何控制django中duration字段的格式,python,django,Python,Django,我正在做一个简单的时钟应用程序。你按一个按钮打进去,然后按它打出去。 我试图显示出击和入击之间的小时/分钟。 代码可以工作,但我想更改代码的输出 我得到的输出如下: 一般来说,我对django和python都很陌生,我想知道如何使这个输出更好,比如x:hours y:minutes或者类似的东西。我绝对不需要毫秒 为清楚起见,代码如下: 该过程的模型为: class Punch(models.Model): employee = models.ForeignKey("tim

我正在做一个简单的时钟应用程序。你按一个按钮打进去,然后按它打出去。 我试图显示出击和入击之间的小时/分钟。 代码可以工作,但我想更改代码的输出

我得到的输出如下:

一般来说,我对django和python都很陌生,我想知道如何使这个输出更好,比如x:hours y:minutes或者类似的东西。我绝对不需要毫秒

为清楚起见,代码如下:

该过程的模型为:

class Punch(models.Model):
     employee = models.ForeignKey("timekeeper.User", on_delete=models.CASCADE,  
          related_name="employee_punch")
     punchDate = models.DateField(default='2020-01-01')
     punchInTime = models.TimeField(default='01:01:01')
     punchOutTime = models.TimeField(null=True)
     worked = models.DurationField(null=True)

     def __str__(self):
         return f"Emp: {self.employee}, Timestamp: {self.punchDate}, IN: {self.punchInTime},  
                OUT:{self.punchOutTime} -- Worked: {self.worked}"
查看代码为:

# Create your views here.
def index(request):
    
    # The punch button was pushed
    if request.method == 'POST':
        r = request.POST.get('punch_button')

        # Record the punch on the Punch table
        if r == "PUNCH IN":
            Punch.objects.create(
                employee=request.user, 
                punchInTime=datetime.now().time(), 
                punchDate=datetime.now().date()
            )
            
        elif r == "PUNCH OUT":
            now = datetime.now()
            p = Punch.objects.filter(employee=request.user, punchDate=now.date()).last()
            p.punchOutTime=now.time()
            
            # Converting punch times to dateTimeFields for math operations
            a = datetime.combine(p.punchDate, now.time())
            b = datetime.combine(p.punchDate, p.punchInTime)
            worked = ( a - b)

            p.worked=worked
            p.save()
            
        return HttpResponseRedirect(reverse("index"))


    # GET method call
    else:

        # Ensure User is authenticated
        if request.user.is_authenticated:
            now = datetime.now()
            dayname = now.strftime("%A")
            
            # If user is working today, send over schedule info.
            try:
                schedule = Schedule.objects.get(employee=request.user, scheduleDate=datetime.today().date())
            except:
                schedule = "NONE"
            
            if schedule != "NONE":
                punches = Punch.objects.filter(employee=request.user, punchDate=datetime.now().date())
                p = Punch.objects.filter(employee=request.user, punchDate=datetime.now().date(), punchOutTime=None)
                
                if p.count() == 0 or (p.count() % 2) == 0:
                    punch_type = "PUNCH IN"
                else:
                    punch_type = "PUNCH OUT"

                
                return render(request, "timekeeper/index.html", {
                    "now": now,
                    "dayname": dayname,
                    "schedule": schedule,
                    "type": punch_type,
                    "count": p.count(),
                    "punches": punches
                })

            # Not scheduled to work today.    
            else: 
                return render(request, "timekeeper/index.html", {
                    "now": now,
                    "dayname": dayname,
                    "schedule": schedule,
                })

        # User is not authenticated
        else:
            return HttpResponseRedirect(reverse("login"))
我是以下HTML:

<table class="table">
    <thead class="thead-dark">
        <tr>
            <th scope="col">IN</th>
            <th scope="col">OUT</th>
            <th scope="col">worked</th>
        </tr>
    </thead>
    <tbody>
        {% for punch in punches %}
        <tr>
           <td>{{ punch.punchInTime }}</td>
           <td>{{ punch.punchOutTime }}</td>
           <td>{{ punch.worked }}</td>
        </tr>
        
        {% endfor %}
    </tbody>
</table>

在里面
出来
工作
{%用于打孔%}
{{punch.punchInTime}
{{punch.punchOutTime}
{{punch.worked}}
{%endfor%}

您可以为此任务创建自己的自定义模板筛选器。检查这个答案