请原谅我对 python 和 web 应用程序的新手状态。
几个小时后,我根本无法让它正常工作。
我想做的就是让表格项目以秒为单位显示时间。
它从views.py中正确打印,但在传递给index.html时似乎进行了一些转换(如console.log上所示)?或者有人可以为我澄清吗?或者请指出我正确的方向?
模型.py:
class dbMachine100RunTime(models.Model):
ID = models.AutoField(primary_key=True)
Machine = models.ForeignKey(Machines, on_delete=models.SET_NULL, null=True, blank=True)
Date = models.DateField(auto_now=False, default=datetime.now)
Time = models.TimeField(auto_now=False, default=datetime.now)
OnOff = models.CharField(max_length=3, default="Error", null=True)
Count = models.IntegerField(default=0, null=True)
def __str__(self):
return str(self.Machine)
视图.py:
def index(request):
EndTime = datetime.now()
TimeDifference = timedelta(hours=1)
StartTime = EndTime-TimeDifference
MachineRuntime = dbMachine100RunTime.objects.filter(Time__range=(StartTime, EndTime))
Machine = MachineRuntime[0].Machine
print(MachineRuntime[0].Date) #Shows date as = 2021-04-25
print(MachineRuntime[0].Time) #Shows time as = 10:30:36
return render(request, 'Machine_Monitor/index.html', {'MachineRuntime': MachineRuntime, 'Machine': Machine})
指数:
{% for item in MachineRuntime %}
<tr>
<td>{{item.Date}}</td>
<td><time step="1">{{item.Time}}</time></td>
<script>
console.log("{{item.Date}}"); <!--Shows as: April 25, 2021 -->
console.log("{{item.Time}}"); <!--Shows as: 10:30 a.m. -->
</script>
</tr>
{% endfor %}