我需要每 X 秒使用数据库中的新信息更新我的网页。这是一个相对较小的应用程序,所以我想 schedule会完成这项工作。
到目前为止我做了什么:
- 从数据库获取最新值
- 传递到模板
- 遵循了计划文档(或者至少我认为我做到了),但当我打印到控制台作为测试时似乎没有发生任何事情。
这是我在 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)
除了计划部分
之外,上面的代码运行良好。没有给出任何错误。
如何每 X
秒打印最后一个 oee
值的更新版本?
最佳答案
您甚至不需要 Javascript。只需添加以下meta refresh
标记到您的模板:
<meta http-equiv="refresh" content="60">
关于python - Django 1.10 - 每 X 秒使用数据库中的数据更新网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51766602/