我需要更新我的网页每X秒与新的信息,从数据库。这是一个相对较小的应用程序,所以我认为日程安排会做这个工作。
我到目前为止所做的事:
这是我在views.py中的代码
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from lineoee.models import Lineoee3
import threading
import time
import schedule
def job():
last_oee1 = oee_list[-1]
print(last_oee1) #test print
def index(request):
context = {}
lines = Lineoee3.objects.all().values('oee')
enter code here
oee_list = list(Lineoee3.objects.all().values_list('oee',
flat=True))
schedule.every(10).seconds.do(job)
last_oee = oee_list[-1]
var = "Current OEE is: "
context = {'lines' : lines, 'var' : var, 'last_oee' : last_oee,}
return render(request, 'lineoee/index.html',context)除schedule part之外,上面的代码运行良好。没有错误。
如何每隔一秒钟打印上一次oee值的更新版本?
发布于 2018-08-09 13:03:51
你甚至不需要Javascript。只需将以下meta refresh标记添加到模板:
<meta http-equiv="refresh" content="60">发布于 2018-08-09 12:19:55
是的,我会在模板lineoee/index.html中留下一个脚本:
<script>
setTimeout(function reload() {location.reload()}, 2000)
</script>https://stackoverflow.com/questions/51766602
复制相似问题