首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django 1.10 -每X秒更新一次数据库数据的网页

Django 1.10 -每X秒更新一次数据库数据的网页
EN

Stack Overflow用户
提问于 2018-08-09 12:07:00
回答 2查看 644关注 0票数 2

我需要更新我的网页每X秒与新的信息,从数据库。这是一个相对较小的应用程序,所以我认为日程安排会做这个工作。

我到目前为止所做的事:

  • 从数据库中获取最新值
  • 传递到模板
  • 遵循时间表文档(或者至少我认为我这样做了),但是当我打印到控制台作为测试时,似乎什么也没有发生。

这是我在views.py中的代码

代码语言:javascript
复制
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值的更新版本?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-09 13:03:51

你甚至不需要Javascript。只需将以下meta refresh标记添加到模板:

代码语言:javascript
复制
<meta http-equiv="refresh" content="60">
票数 3
EN

Stack Overflow用户

发布于 2018-08-09 12:19:55

是的,我会在模板lineoee/index.html中留下一个脚本:

代码语言:javascript
复制
<script>
    setTimeout(function reload() {location.reload()}, 2000)
</script>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51766602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档