我正在Django中创建一个产品页面(10个产品),其中我需要以某种速度增加所有产品中的虚拟视图。
给定日期内任何产品的虚拟视图(D)N为 D = N * Y * X
,其中已经为每种产品随机选择了每种产品的X(此后也将保持不变),而Y每天从一组中随机抽取不同的产品。
因此,基本上,我需要在每次刷新页面时增加观看次数,因此, D/24*60
将使我们每分钟的观看次数增加。
我怎样才能以每分钟一定的速度增加观看次数?
Models.py
class Products(models.Model):
title = models.CharField(max_length = 100)
description = models.TextField()
views = models.IntegerField(null = True)
image = models.ImageField(upload_to = 'productImages/')
dateUploaded = models.DateField(auto_now_add=True)
Daytimestamp = models.DateField(auto_now_add=True)
actualViews = models.IntegerField(null = True)
class Meta:
ordering = ['title']
class Views(models.Model):
X = ArrayField(models.IntegerField(blank = True))
Y = ArrayField(models.FloatField(blank= True))
Viewtimestamp = models.DateTimeField(auto_now_add=True)
views.py中的我的主页功能是
def home(request):
products = Products.objects.all()
views = Views.objects.all()
# caculate Days(N)
delta = (datetime.date.today()-products[0].dateUploaded)
if delta.days == 0:
Day = 1
else:
Day = delta.days + 1
# if next day then change Y
if datetime.date.today() != products[0].Daytimestamp:
makeY() #this function changes Y array
for product in products:
product.Daytimestamp = datetime.date.today()
product.save()
# give minutes passed since last update
delta = datetime.datetime.now(timezone.utc) - views[0].Viewtimestamp
print("Seconds passed", delta.seconds)
v0 = views[0]
v0.Viewtimestamp = datetime.datetime.now(timezone.utc)
v0.save()
minutes_passed = (delta.seconds)/60
for product in products:
DummyView = Day * views[0].X[product.id-1] * views[0].Y[product.id-1]
DummyViewPerMinute = (DummyView/(24*60))
product.views += (minutes_passed * DummyViewPerMinute)
product.save()
return render(request, '../templates/home.html', {'Products': products})
我一直在跟踪上次更新视图的时间戳(Viewtimestamp),并增加了经过的分钟数,但视图似乎没有增加。