在我的 Django 应用程序中,我有一个视图,可以将数据提交到我的任务模型的数据库。其中一个字段包括时间。每当我提交该数据,然后将其读回我的应用程序时,它看起来是正确的(即我的索引页面,其中包含所有对象的列表)。但是,当我查看数据库条目时,它们都存储在我输入后 5 小时的时间。让我感到困惑的是它们可能如何输入错误以及为什么它在我的索引中正确显示。我检查了我添加它的位置,如下所示。
hour = int(request.POST['time'][:2] #request.POST['time'] is formatted HH:MM AM
minute = int(request.POST['time'][3:5]
pmOffset = 0
if 'PM' in request.POST['time']:
pmOffset = 12
print task.date
task.date = datetime.now().replace(hour=hour+pmOffset, minute=minute)
print task.date
#some other fields
print task.date
task.save()
print task.date
如果我说在我的表单中输入 07:00 AM,那么这些打印语句会在上午 7 点出现,但存储的任务中包含中午。任何人都知道为什么会发生这种情况?我完全感到困惑。如果您需要更多信息,请询问。